How to speed up searching in a text file?

activities
bestpractices

#1

I am trying to search for some keywords in a big string (over 200 000 characters). I know that the keyword will be after the position 130 000. I am using Substrings to elimintate the firs 130 000 characters, but still it takes nearly 30 seconds as it anyway needs to go through all the 130 000 characters to be able to trim them. How can I speed up the search?


#2

Hi @Jakub,

Boyer-Moore is a string searching algorithm. Compared to IndexOf, this is a faster way to search for substrings in a string. It avoids checking most positions in the source string. The implementation here uses constant characters.

Please take a look at this:
https://www.dotnetperls.com/boyer-moore


#3

This is just me, and depends on what you need to do with that string you are searching for, but I would use string manipulation like .Split, .Replace, or .Contains
or
I would use LINQ/Lambda to filter the lines of the string to only the lines that contain the specific string.
For example,
(From line In text.Split(vblf(0)) Where line.Contains(“string”) Select line).ToArray()
Then from there you can loop through all lines with that string in it and even filter it further using multiple criteria.