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

后端 未结 4 765
甜味超标
甜味超标 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:24

    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"

提交回复
热议问题