title: c# searching large text file
body: <p>I am trying to optimize the search for a string in a large text file (300-600mb). Using my current method, it is taking too long.</p>
  
  <p>Currently I have been using <code>IndexOf</code> to search for the string, but the time it takes is way too long (20s) to build an index for each line with the string.</p>
  
  <p>How can I optimize searching speed? I've tried <code>Contains()</code> but that is slow as well. Any suggestions? I was thinking regex match but I don't see that having a significant speed boost. Maybe my search logic is flawed</p>
  
  <p>example </p>
  
  <pre><code>while ((line = myStream.ReadLine()) != null)
  {
      if (line.IndexOf(CompareString, StringComparison.OrdinalIgnoreCase) &gt;= 0)
      {
          LineIndex.Add(CurrentPosition);
          LinesCounted += 1;
      }
  }
  </code></pre>