4

I want to add one space after every two characters, and add a character in front of every single character.

This is my code:

string str2;
str2 = str1.ToCharArray().Aggregate("", (result, c) => result += ((!string.IsNullOrEmpty(result) && (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString());

I have no problems separating every two characters with one space, but how do I know if the separated string has an individual character, and add a character infront of that character?

I understand that my question is confusing as I'm not sure how to put what I want in words.. So I'll just give an example:

I have this string:

0123457

After separating every two characters with a space, I'll get:

01 23 45 7

I want to add a 6 infront of the 7.

Note: Numbers are dependent on user's input, so it's not always the same.

Thanks.

3
  • 1
    Iterate across the characters in the input string and use a StringBuilder to build the desired output. Jul 7, 2011 at 19:08
  • How do I do that? Sorry, I'm quite new to this :/
    – Rove
    Jul 7, 2011 at 19:25
  • The question is a little ambiguous. Do you mean: split your string into chunks of 2 characters, separate each pair with a space. If there's an odd character left over (at the end), prefix it with another (specific) character to make it into a final pair.
    – Rob
    Jul 7, 2011 at 21:55

4 Answers 4

21
[TestMethod]
public void StackOverflowQuestion()
{
    var input = "0123457";
    var temp = Regex.Replace(input, @"(.{2})", "$1 ");
    Assert.AreEqual("01 23 45 7", temp);
}
1
  • 3
    a TrimEnd(" ") is required. Anyway, great solution.
    – mmmmmm
    Jun 20, 2013 at 8:02
4

Try something like this:

static string ProcessString(string input)
{
    StringBuilder buffer = new StringBuilder(input.Length*3/2);
    for (int i=0; i<input.Length; i++)
    {
        if ((i>0) & (i%2==0))
            buffer.Append(" ");
        buffer.Append(input[i]);
    }
    return buffer.ToString();
}

Naturally you'd need to add in some logic about the extra numbers, but the basic idea should be clear from the above.

1
  • this way he could to the processing he wants Dec 23, 2011 at 10:56
2

May be you can try, if i right understand your request,

String.Length % 2

if result is 0, you done with first iteration, if not, just add a character infront of last one.

1

I think this is what you asked for

string str1 = "3322356";
            string str2;

            str2 = String.Join(" ", 
                str1.ToCharArray().Aggregate("",
                (result, c) => result += ((!string.IsNullOrEmpty(result) &&
                    (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString())
                    .Split(' ').ToList().Select(
                x => x.Length == 1 
                    ? String.Format("{0}{1}", Int32.Parse(x) - 1, x) 
                    : x).ToArray());

result is "33 22 35 56"

3
  • 1
    Is this a serious suggestion? Jul 7, 2011 at 20:55
  • Well.. it worked hahah. edit: and thanks david, I'll look more into stringbuilder, seems pretty nice!
    – Rove
    Jul 8, 2011 at 1:05
  • 1
    Why do you accept the joke answer? I mean I get the joke but this is not real code, it's just a bit of fun. Or do you really believe that all problems must be solved with Linq? Jul 8, 2011 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.