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

后端 未结 14 1832
灰色年华
灰色年华 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 09:04

    As Wayne Cornish already said: Reading line by line might be the best approach.

    If you read for instance the entire file into a string and then search with a regular expression, it might be more elegant, but you'll create a large string object.

    These kind of objects might cause problems, because they will be stored on the Large Object Heap (LOH, for objects above 85.000 bytes). If you parse many of these big files and your memory is limited (x86), you are likely to run into LOH fragmentation problems.

    => Better read line by line if you parse many big files!

提交回复
热议问题