根据一些关键词将文件文本拆分为数组 [英] Split the text of file into array based on some key word

查看:86
本文介绍了根据一些关键词将文件文本拆分为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用streamreader阅读文本文件。



我想拆分文件的文本为数组或任何集合的字符串。



例如



学生:Prasad,

xyz

abc

这里有关Prasad的一些文字。

学生:Raj

abc

ijk

这里有关于Raj的一些文字

学生:Shaan

xxx

这里有关于Shaan的一些文字



现在......



我想要字符串数组如下< br $>


数组[ 0 ] =  学生:Prasad,
xyz
abc
这里有关于Prasad的一些文字。


array [ 1 ] = 学生: Raj
abc
ijk
这里有关Raj的一些文字


array [ 2 ] = 学生:Shaan
xxx
这里的一些文字关于Shaan

解决方案

使用String.Split:

< pre lang =cs> string inp = File.ReadAllText( @ d:\Temp\xxx.txt);
string [] sections = inp.Split( new string [] { 学生:},StringSplitOptions.RemoveEmptyEntries);

或正则表达式:

  string  inp = File.ReadAllText( @  D:\Temp \ xxx.txt); 
string [] sections = Regex.Split(inp, 学生:,);


Hi,

I am reading a text file using streamreader.

I want to split the text of file into string of arrays or any collection.

e.g.

Student: Prasad,
xyz
abc
some text here about Prasad.
Student: Raj
abc
ijk
some text here about Raj
Student: Shaan
xxx
some text here about Shaan

Now...

I want the string array as below

array[0] ="Student: Prasad,
xyz
abc
some text here about Prasad."

array[1] ="Student: Raj
abc
ijk
some text here about Raj"

array[2]="Student: Shaan
xxx
some text here about Shaan
"

解决方案

Either use String.Split:

string inp = File.ReadAllText(@"D:\Temp\xxx.txt");
string[] sections = inp.Split(new string[] {"Student: "},StringSplitOptions.RemoveEmptyEntries);

Or a regex:

string inp = File.ReadAllText(@"D:\Temp\xxx.txt");
string[] sections = Regex.Split(inp, "Student: ",);


这篇关于根据一些关键词将文件文本拆分为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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