简体   繁体   English

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

[英]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.

相关问题 c# - 在多个数组中拆分字符串的最佳方法是什么 - c# - What is the best way to split string in multiple arrays 在C#中使用String.Split()方法或Linq Lambda表达式从给定字符串中查找拆分字符长度的最佳方法是什么 - What is the best way to find length of split characters from the given string by using String.Split() Method or Linq Lambda Expression in C# 在 C# 中解析此字符串的最佳方法是什么? - What is the best way to parse this string in C#? 将“ [[a],{b},{c}]”拆分为字符串数组“ a,b,c”的最佳方法是什么 - What is the best way to split “[{a},{b},{c}]” into a string array “a,b,c” 拆分字符串的最佳方法是什么 - what is the best way to split a string c#-将字符串拆分为2个数组的有效方法是什么 - c# - what is the efficient way to split string to 2 arrays 在C#中使用此字符串值的最佳方法是什么? - What is the best way of using this string value in c#? 删除部分字符串的最佳方法是什么? (C#) - What's the best way to remove portions of a string? (C#) 在C#中,从字符串中解析出这个值的最佳方法是什么? - In C#, what is the best way to parse out this value from a string? 转换VB if语句将字符串返回到C#的最佳方法是什么 - What is the best way to convert VB if statement that return a string to C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM