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

特定の条件を満たす行をスキップするだけでよいようです。

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 に答える
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 に答える
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 に答える