2

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?

0

3 Answers 3

4

If those integers are 1-based indices within the string (i.e. 2 = 2nd character), then you could do this:

string s = "ABD";
int[] oneBasedIndices = new [] { 2, 3, 1 };

string result = String.Join(String.Empty, oneBasedIndices.Select(i => s[i-1]));

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 each int element 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Nice, although I prefer using String.Empty instead of ""
@bitxwise - you're right, I tend to change my mind on that each week, but String.Empty at least says "I know this is empty", whereas "" doesn't convey that same intent ("did they forget the string?"). Updated.
I cant believe that this functionality can be implemented just with 1 line of code in c#!! (I am a Java developer and learning c# now a days.). Impressed.. :)
@Nirmit, the string.Join() method is just a static method which builds a concatenated string of the ToString() function of each element. Internally, it uses a loop and StringBuilder.
@Jon - I got idea behind string.join(). But i am more impressed with LINQ functionality ( oneBasedIndices.Select(i => s[i-1])) ). In fact I start using this in my current project. :) Thanks.
1

First make a copy of the string. The copy will never change; it serves as your reference to what the values used to be.

Then loop through the original string one character at a time using a for loop. The counter in the for loop is the position of which character in the original string we are replacing next. The counter is also the index into the array to look up the position in the original string. Then replace the character at that position in the original string with the character from the copied string.

string orig = "ABD";
int[] oneBasedIndices = new [] { 2, 3, 1 };

string copy = orig;
for ( int i = 0; i < orig.Length; i++ )
{
    orig[i] = copy[ oneBasedIndices[i] - 1 ];
}

There you have it. If the indices are zero based, remove the - 1.

3 Comments

This is probably a better example for a beginner as you need to get for loops before you can visualise lambda transformations.
It is also good for beginners to see that there are many, many ways to accomplish the same task. Eventually, the challenge is deciding which way to use and why, instead of just figuring a way to do it.
The indexer on a string does not have a setter, orig[i] = something; won't work. Because strings in C# are immutable, you will need to build a new string.
0

Napkin code again...

    string result = "ABD"; // from last question
    var indecies = new []{ 1,2,0 };

    string result2 = indecies.Aggregate(new StringBuilder(), 
       (sb, i)=>sb.Append(result[i]))
       .ToString();

or a different version (in hopes of redeeming myself for -1)

StringBuilder sb = new StringBuilder();
for(int i = 0; i < indecies.Length; i++)
{
   sb.Append(result[i]); // make [i-1] if indecies are 1 based.
}
string result3 = sb.ToString();

2 Comments

Message=Index was outside the bounds of the array. :-(
You're right, I copied the 2,3,1 indecies from your question. I should have noticed it wasn't a 0 based index (fixed). Suppose I'll stop napkin-coding answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.