在c#中拆分字符串的最佳方法是什么?

时间:2016-04-12 05:45:58

标签: c# split

我有一个看起来像那样的字符串" a,b,c,d,e,1,4,3,5,8,7,5,1,2,6 ....等等上。 我正在寻找分裂它的最好方法,使它看起来像那样:

a b c d e

1 4 3 5 8

7 5 1 2 6

先谢谢

1 个答案:

答案 0 :(得分:2)

假设你有一个固定数量的列(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();

首先,我将字符串分割为字符,然后将结果分组为5个项目的块,并将这些块选择为数组。

结果:

a    b    c    d    e
11   45   34  33   79
65   75   12   2    6

将该结果写入您必须

的文件中
using (System.IO.StreamWriter writer =new System.IO.StreamWriter(path,false))
{
    foreach (string[] line in Result)
    {
        writer.WriteLine(string.Join("\t", line)); 
    }
}; 
相关问题