I have to count string occurences in one very loooong string (about 30mb in plain text) I use the following code for now: int count = new Regex(Regex.Escape(stringThatIamLookingFor)).Matches(stringToSearchIn).Count;
but is is too slow. It takes about 3 minutes on i7 and 16gb ram. The example data is:
43.996442,-31.768039
43.996432,-31.768039
43.996432,-31.768049
43.996422,-31.768049
43.996422,-31.768059
I want to count (for example) .7
Is there faster way than regeex?
The fastest function so far is: (I need to check only two chars.)
public int countOccurences2(string source, char charOne, char charTwo)
{
int count = 0;
for (int i=0; i<=source.Length-1; i=i+2)
if (source[i] == charOne && source[i + 1] == charTwo) { count++; }
return count;
}
from this question: How would you count occurrences of a string within a string?
the following code seems the most performing:
int count = 0, n = 0;
if(substring != "")
{
while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
{
n += substring.Length;
++count;
}
}
solution provided by Richard Watson in the mentioned question
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.