我有一个文本文件,其中每隔几行后就会在行开头重复一个特定字符。没有。之间的线数不固定。我能够找出发生这种情况的那些行。我想读一下中间的那些行。
using (StreamReader sr = new StreamReader(@"text file"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("some character"))
因为下次该字符出现时,代码保持不变。我无法阅读之间的那些行
For eg.
Condition at the begining of a line
Next line
Next line
Condition at the begining of a line
Next Line
Next Line
Next Line
Next Line
Condition at the begining of a line
我必须读中间的几行。每次情况都保持相同。谢谢。
我假设您想要解析文本文件并每次处理条件之间的所有文本块。
基本上,您读取每一行,检查第一个条件,该条件表示一个“块”的开始,并继续读取这些行,直到找到另一个条件,该条件表示该块的结束(以及另一个块的开始)。
清洗、冲洗,重复。
一个简单的例子:
using (var reader = new StreamReader(@"test.txt"))
{
var textInBetween = new List<string>();
bool startTagFound = false;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (String.IsNullOrEmpty(line))
continue;
if (!startTagFound)
{
startTagFound = line.StartsWith("Condition");
continue;
}
bool endTagFound = line.StartsWith("Condition");
if (endTagFound)
{
// Do stuff with the text you've read in between here
// ...
textInBetween.Clear();
continue;
}
textInBetween.Add(line);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)