我有以下字符串:分割字符串成2 C#塊
string input ="this is a testx";
我需要把空格去掉,然後拆分輸入兩個大塊,這樣我就可以單獨每兩個字母的過程:
th的是在ES TX
我試圖與刪除空格:
input=input.Remove(input.IndexOf(' '),1);
Ť母雞我不能做與分裂...
我有以下字符串:分割字符串成2 C#塊
string input ="this is a testx";
我需要把空格去掉,然後拆分輸入兩個大塊,這樣我就可以單獨每兩個字母的過程:
th的是在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);
}
什麼代碼中有你試過嗎?你是否收到異常或不正確的結果? – 2014-09-22 00:25:22
問題是什麼? – Dmitry 2014-09-22 00:25:33