[英]what is the best way to split string in c#
i have a string that look like that "a,b,c,d,e,1,4,3,5,8,7,5,1,2,6.... and so on. i am looking for the best way to split it and make it look like that: 我有一个看起来像这样的字符串“ a,b,c,d,e,1,4,3,5,8,7,5,1,2,6 ....依此类推。我正在寻找拆分它并使它看起来像这样的最佳方法:
abcde abcde
1 4 3 5 8 1 4 3 5 8
7 5 1 2 6 7 5 1 2 6
thanks in advanced 提前致谢
Assuming, that you have a fix number of columns (5): 假设您有固定的列数(5):
string Input = "a,b,c,d,e,11,45,34,33,79,65,75,12,2,6";
int i = 0;
string[][] Result = Input.Split(',').GroupBy(s => i++/5).Select(g => g.ToArray()).ToArray();
First I split the string by , character, then i group the result into chunks of 5 items and select those chunks into arrays. 首先,我将字符串分割为,字符,然后将结果分组为5个项目的块,然后将这些块选择为数组。
Result: 结果:
a b c d e
11 45 34 33 79
65 75 12 2 6
to write that result into a file you have to 要将结果写入文件,您必须
using (System.IO.StreamWriter writer =new System.IO.StreamWriter(path,false))
{
foreach (string[] line in Result)
{
writer.WriteLine(string.Join("\t", line));
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.