Search code examples
c#stringsearchlinestreamreader

How to find specific line with unique string content in large text document


I'm trying to check if large text document about 500 000 lines contains specific line, and problem is if I find it this way:

string searchLine = "line 4";

using (StreamReader sr = new StreamReader(filePath)) 
{
   string contents = sr.ReadToEnd();
   if (contents.Contains(searchLine))
   {
      Console.WriteLine("line exist");
   }
   else
   {
      Console.WriteLine("line does not exist");
   }
}

and document content is and I do not accept writing duplicates to it, all string are unique:

line 1
line 2
line 3
line 4
line 5
line 47

So I got answer that's "line exist" for "line 4" right, but then if I remove it from the order, and check file for same string "line 4" again, it says that the "line exist", because seems like it founds all 4 numbers in text file content, and only if I remove "line47", then "line does not exist".

So I'm wondering how to find specific line with unique string content in large text document.


Solution

  • sr.ReadToEnd(); does not read the file line by line but reads all characters from the current position to the end of the stream.

    While the Readline() method reads a line of characters from the current stream and returns the data as a string

    The Readline() method will read the file line by line like so:

    string currentLine;
    bool exist = false;
    
    using (StreamReader sr = new StreamReader(filepath))
    {
        while ((currentLine = sr.ReadLine()) != null)
        {
            if (currentLine == "line 4")
                exist = true;                       
        }
     }
    
     Console.WriteLine(exist ? "line exist" : "line does not exist");
    

    Alternatively you can also compare with:

    string.Equals(currentLine, "line 4")
    

    instead of

    currentLine == "line 4"