我必须在一个非常loooong字符串中计算字符串出现次数(纯文本约30mb)
我现在使用以下代码:
int count = new Regex(Regex.Escape(stringThatIamLookingFor)).Matches(stringToSearchIn).Count;
但是太慢了。在i7和16gb ram上大约需要3分钟。
示例数据是:
43.996442,-31.768039
43.996432,-31.768039
43.996432,-31.768049
43.996422,-31.768049
43.996422,-31.768059
我想数(例如).7
有没有比regeex更快的方式?
目前最快的功能是:(我只需要检查两个字符。)
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;
}
答案 0 :(得分:3)
来自这个问题: How would you count occurrences of a string within a string?
以下代码似乎效果最佳:
int count = 0, n = 0;
if(substring != "")
{
while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1)
{
n += substring.Length;
++count;
}
}
Richard Watson在上述问题中提供的解决方案