You can use String.IndexOf, but be sure to use StringComparison.Ordinal or it can be one order slower.
private string Search2(int startPos, string startMatchString, string endMatchString, string response) {
int startMarch = response.IndexOf(startMatchString, startPos, StringComparison.Ordinal);
if (startMarch != -1) {
startMarch += startMatchString.Length;
int endMatch = response.IndexOf(endMatchString, startMarch, StringComparison.Ordinal);
if (endMatch != -1) { return response.Substring(startMarch, endMatch - startMarch); }
}
return string.Empty;
}
A 1000-fold search for a string of about 40% of an 183 KB file took about 270 milliseconds. Without StringComparison.Ordinal, it took about 2000 milliseconds.
Finding 1 time with your method took more than 60 seconds, as it creates a new line (O (n)) at each iteration, making your method O (n ^ 2).
source
share