3

我有一个文本文件,其中每隔几行就会在行首重复一个特定字符。没有。之间的行数不固定。我能够找出发生这种情况的那些行。我想阅读介于两者之间的那些行。

 using (StreamReader sr = new StreamReader(@"text file"))
 {
     string line;
     while ((line = sr.ReadLine()) != null)
     {
         if (line.StartsWith("some character"))

因为下一次,这个字符出现,代码保持不变。我无法阅读介于两者之间的那些行

例如。

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

我必须阅读中间的行。情况每次都保持不变。谢谢。

4

6 回答 6

4

我假设您要解析文本文件并每次处理条件之间的所有文本块。

基本上你阅读每一行,检查第一个条件,它表示“块”的开始,并继续阅读这些行,直到你找到另一个条件,它表示块的结束(以及另一个块的开始)。

洗涤,冲洗,重复。

一个简单的例子:

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);
    }
}
于 2012-06-25T19:27:21.810 回答
4

看来您只需要跳过满足特定条件的行。

var lines = System.IO.File.ReadLines(@"text file")
            .Where (line => ! line.StartsWith("Condition");

foreach(string line in lines)
{
  // do your stuff
}
于 2012-06-25T19:37:27.300 回答
1

如果我理解正确,您想在“块内或块外”的条件之间切换:

{
  string line;
  int inside = 0;
  while ((line = sr.ReadLine()) != null)
  {
     if (line.StartsWith("some character"))
     {
         inside = !inside;
         // If you want to process the control line, do it here.
         continue;
     }
     if (inside)
     {
         // "line" is inside the block. The line starting with "some character"
         // is never here.
     }
     else
     {
         // Well, line is outside. And the control lines aren't here either.
     }
  }
}
于 2012-06-25T19:33:24.143 回答
1

这是我经常使用的一种方法,用于读取对于 XDocument 来说太大的 xml 文件。根据您的需要对其进行修改将非常容易。

public static void ReadThroghFile(string filePath, int beingLinesToSkip, string endingMarker, Action<string> action)
{
   using (var feed = new StreamReader(filePath))
   {
       var currentLine = String.Empty;
       var ongoingStringFromFeed = String.Empty;

       for (var i = 0; i < beingLinesToSkip; i++) feed.ReadLine(); 

       while ((currentLine = feed.ReadLine()) != null)
       {
           ongoingStringFromFeed += currentLine.Trim();
           if (!currentLine.Contains(endingMarker)) continue;

           action(ongoingStringFromFeed);
           ongoingStringFromFeed = String.Empty;
       }
    }
}
于 2012-06-25T19:36:02.420 回答
0

除非我遗漏了什么,否则您似乎正在从 StreamReader读取“每一行”并检查您的“条件”是否满足。所以我不明白你的问题“我想阅读中间的那些行。”!!!!

于 2012-06-25T19:23:11.407 回答
0

难道你不能做你想做的事吗?

using (StreamReader sr = new StreamReader(@"text file"))
{
 string line;
 while ((line = sr.ReadLine()) != null)
 {
     if (line.StartsWith("some character"))
     {
        //Get rid of line
     }
     else
     {
       // Do stuff with the lines you want
     }  
于 2012-06-25T19:23:33.780 回答