Add one space after every two characters and add a character infront of every single character

后端 未结 4 769
甜味超标
甜味超标 2021-01-20 04:05

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         


        
4条回答
  •  粉色の甜心
    2021-01-20 04:22

    Try something like this:

    static string ProcessString(string input)
    {
        StringBuilder buffer = new StringBuilder(input.Length*3/2);
        for (int i=0; i0) & (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.

提交回复
热议问题