如何按空格分割数据? [英] How to split data by whitespace?

查看:65
本文介绍了如何按空格分割数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文中有数据:



 WS 6 
WH 11
WD 6
WC 8
WN 9

NS 7
NH 2
ND 6
NC 5

NN 4
ES 6
EH 11
ED 6
EC 8
EN 9





我希望将它们分成如下字符串数组:

 string [0] array = 
WS 6
WH 11
WD 6
WC 8
WN 9

string [1] array =
NS 7
NH 2
ND 6
NC 5

string [2] array =
NN 4
ES 6
EH 11
ED 6
EC 8
EN 9





我的尝试:



我一直在做:

使用(StreamReader reader = new StreamReader(uploadedFile.InputStream))
{

string read = reader.ReadToEnd();
string [] Array = Regex.Split(读,\ n);
}





但它无法完成,请帮助我。

解决方案

使用string.Split



String.Split方法(String [],StringSplitOptions)(系统) [ ^ ]



 string [ ] x = text.Split(new char [] {''},StringSplitOptions.RemoveEmptyEntries); 





如果你有多行你可能需要在新行上拆分然后遍历每一行并拆分空间。或者只是逐行读取数据并在空间上分割,而不是在一个块中读取。


假设你的文本使用\ n作为换行符。

 var text =WS 6 \ nW H 11 \ nW D 6 \ nW C 8 \ nW N 9 \ nnN S 7 \ nN H 2 \ nN D 6 \ nN C 5 \ nN n N 4 \ nE S 6 \ nE H 11 \ nE D 6 \ nE C 8 \ nE N 9 \ n; 



然后你可以像这样拆分这个文本:

 var array = Regex.Split(text,\ n \ n ); 


当您想要分割1个字符时,使用Regex.Split进行拆分有点矫枉过正。尝试 var array = read.Split('\ n');



小心'\ r',因为它有时可以伴随\ n取决于字符串的来源。拆分后修剪弦乐可能是一个想法。 Linq会更轻松:



 使用(StreamReader reader =  new  StreamReader(uploadedFile.InputStream))
{
string read = reader.ReadToEnd( );
string [] Array = read.Split(read, \ n)。选择(s => s.Trim({' '' \ r'})。ToArray();
}


I have data in a text:

W S 6
W H 11
W D 6
W C 8
W N 9

N S 7
N H 2
N D 6
N C 5

N N 4
E S 6
E H 11
E D 6
E C 8
E N 9



and I want to split them to a string array like:

string[0] array=
W S 6
W H 11
W D 6
W C 8
W N 9

string[1] array=
N S 7
N H 2
N D 6
N C 5

string[2] array=
N N 4
E S 6
E H 11
E D 6
E C 8
E N 9



What I have tried:

I had been do:

using(StreamReader reader = new StreamReader(uploadedFile.InputStream))
                {
                   
                    string read = reader.ReadToEnd();
                    string[] Array = Regex.Split(read,"\n");
                }



But It cannot done, Pleas help me.

解决方案

Use string.Split

String.Split Method (String[], StringSplitOptions) (System)[^]

string[] x = text.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);



If you have multiple lines you might need to split on the new line then go through each line and split on the space. Or just read the data line by line and split on space rather than reading it in one chunk.


It is assumed that your text uses \n as line break.

var text = "W S 6\nW H 11\nW D 6\nW C 8\nW N 9\n\nN S 7\nN H 2\nN D 6\nN C 5\n\nN N 4\nE S 6\nE H 11\nE D 6\nE C 8\nE N 9\n";


Then you could split this text like this:

var array = Regex.Split(text, "\n\n");


Splitting with a Regex.Split is kinda overkill when you want to split by 1 char. Try var array = read.Split('\n');.

Beware of '\r' as it can sometime accompany \n depending on the source of the string. It might be an idea to trim the strings once split. Linq would be easiers:

using(StreamReader reader = new StreamReader(uploadedFile.InputStream))
    {
        string read = reader.ReadToEnd();
        string[] Array = read.Split(read,"\n").Select(s=>s.Trim({' ','\r'}).ToArray();
    }


这篇关于如何按空格分割数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆