How do you search a large text file for a string without going line by line in C#?

后端 未结 14 1809
灰色年华
灰色年华 2020-12-15 08:23

I have a large text file that I need to search for a specific string. Is there a fast way to do this without reading line by line?

This method is extremely slow beca

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 08:57

    You should be able to read the file character by character matching each character in the search string until you reach the end of the search string in which case you have a match. If at any point the character you've read doesn't match the character you're looking for, reset the matched count to 0 and start again. For example (****pseudocode/not tested****):

    byte[] lookingFor = System.Text.Encoding.UTF8.GetBytes("hello world");
    int index = 0;
    int position = 0;
    bool matchFound = false;
    
    using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
    {
      while (fileStream.ReadByte() == lookingFor[index])
      {
        index++;
    
        if (index == lookingFor.length) 
        {
           matchFound = true;
           position = File.position - lookingFor.length;
           break;
        }
      }
    }
    

    That is one of many algorithms you could use (although it may be off by one with the length check). It will only find the first match so you probably want to wrap the while loop in another loop to find multiple matches.

    Also, one thing to note about reading the file line by line is that if the desired string to match spans lines you're not going to find it. If that's fine then you can search line by line but if you need search strings to span lines you'll want to use an algorithm like I detailed above.

    Finally, if you're looking for best speed, which it sounds like you are, you'll want to migrate the code above to use a StreamReader or some other buffered reader.

提交回复
热议问题