每两个字符后面添加一个空格,并添加每一个字符一个字符盈 [英] Add one space after every two characters and add a character infront of every single character

查看:232
本文介绍了每两个字符后面添加一个空格,并添加每一个字符一个字符盈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每两个字符后添加一个空格,并在每一个字符前加一个字

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

这是我的代码:

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:

我有这个字符串:

0123457

每两个字符用空格分隔后,我将获得:

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

01 23 45 7

$ ; b
$ b

我想补充一个6盈方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.

感谢。

推荐答案

我觉得这就是你要求

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());



结果是33 22 35 56

result is "33 22 35 56"

这篇关于每两个字符后面添加一个空格,并添加每一个字符一个字符盈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆