title: Finding text between two words in string in c#
body: <p>I have file which contain messages.Message Start with "A1" and ends with Z1.</p>
  
  <p>below is file content</p>
  
  <p>A1 s=10 y=10 z=120 Z1 VV CCZ</p>
  
  <p>A1 55 77 88 99 Z1 qq  KK A1</p>
  
  <p>uuu Z1 A1 LL KK ZZ  Z1 SS </p>
  
  <p>now you can see message are split in multiple Line.i need to extract all message from file by reading .txt file line by line.</p>
  
  <p>Output will be list of string messages</p>
  
  <p>A1 s=10 y=10 z=120 Z1</p>
  
  <p>A1 55 77 88 99 Z1</p>
  
  <p>A1 uuu Z1</p>
  
  <p>A1 LL KK ZZ  Z1</p>
  
  <p>solution</p>
  
  <p>public void ProcessFile()
          {</p>
  
  <pre><code>        string _startingWord = "A1";
          string _endingWord = "Z1";
          bool _waitForlastWord = false;
  
          StringBuilder msg = new StringBuilder();
  
          string line;
  
          // Read the file and display it line by line.
          System.IO.StreamReader file =new System.IO.StreamReader(@"G:\CS Session\Test.txt");
          while ((line = file.ReadLine()) != null)
          {
              var message = line.Split(' ').ToList();
              if (message.Count(x =&gt; x == _startingWord) &gt; 0 || message.Count(x =&gt; x == _endingWord) &gt; 0 || _waitForlastWord)
              {
  
                 bool startingFound = false;
                 if (_waitForlastWord)
                  {
                      startingFound = true;
                  }
  
  
                 foreach (var wrd in message)
                 {
                     if (!startingFound)
                     {
                         if (wrd == _startingWord)
                         {
                             startingFound = true;
                         }
                     }
  
                     if (startingFound)
                     {
                         msg.Append(" " + wrd);
  
                         if (wrd == _endingWord)
                         {
                             startingFound = false;
  
  
                             Console.WriteLine(msg.ToString());
                             msg = new StringBuilder();
                         }
                     }
                 }
                 if (! (msg.ToString()==string.Empty))
                 {
                     _waitForlastWord = true;
                 }
              }
  
  
  
          }
  
          file.Close();
  
  
          System.Console.ReadLine();
      }
  </code></pre>