将字符串拆分为2个C#的块
本文关键字:的块 2个 字符串 拆分 | 更新日期: 2023-09-27 18:23:46
我有以下字符串:
string input ="this is a testx";
我需要去掉空格,然后将输入分成两块,这样我就可以单独处理每两个字母:
这是在es tx
我试图用删除空格
input=input.Remove(input.IndexOf(' '),1);
然后我不能做太多的分裂。。。
IEnumerable<string> output = input
.Replace(" ", string.Empty)
.Select((ch, i) => new{ch, grp = i/2})
.GroupBy(x => x.grp)
.Select(g => string.Concat(g.Select(x => x.ch)));
或者更明智地说:)
input = input.Replace(" ", string.Empty);
IEnumerable<string> output =
Enumerable.Range(0, input.Length / 2).Select(x => input.Substring(x * 2, 2));
您可以使用如下输出:
foreach(var item in output)
{
Console.WriteLine(item);
}