Thanks for the help with my question about making an array of booleans into a string. The code is now working good. Now I have another problem. Maybe somebody would like to try. I think I could come up with a solution but if so I’m 99 percent sure that it would be not so simple as the answers I have seen from people here.
What I have is the string “ABD” from my question here. I also have an array of integers. For example [0] = 2, [1] = 3 and [2] = 1. I would like to find a way to apply this to my string to reorder the string so that the string changes to BDA.
Can anyone think of a simple way to do this?
If those integers are 1-based indices within the string (i.e. 2 = 2nd character), then you could do this:
NB: If you are using a version less than C# 4.0, you need to put a
.ToArray()on the end of the select.What this is doing is going through your
int[]and with eachintelement picking the character in the string at that position (well -1, as the first index in an array is 0, but your example starts at 1).Then, it has to do a
String.Join()to turn that collection of characters back into a String.As an aside, I’d recommend downloading LINQPad (no connection) – then you can just paste that code in there as a C# Program, and at any point type
variable.Dump();(e.g.result.Dump();at the end) and see what the value is at that point.