Hi,
I'm wondering what is faster way to find string in another string(filled from 350kb text file) using string.IndexOf method or using regular expression.
thanks j
Printable View
Hi,
I'm wondering what is faster way to find string in another string(filled from 350kb text file) using string.IndexOf method or using regular expression.
thanks j
I don't know if its faster, but you can try this:
VB Code:
Private Function FindIndex(data as String, search as String) as Int32 Dim found as Boolean = False Dim i as Int32 For i = 0 to data.Length - 1 If data.SubString(i, search.Length) = search found = True Exit For End If Next If found Then Return i End If Return -1 End Function
using IndexOf is faster that using RegEx because the RegEx compiles the pattern before searching
Regex is handy to search for patterns of text, not one particular string that you know will come up. Like searching for a phone number. You can use Regex to match on a phone number pattern and have it return all phone numbers in the file that matches the pattern, instead of coding the phone number that you want to search for and using .IndexOf or some other method. If its not a pattern or special kind of string you want to search for, then Regex shouldn't be necessary.