我正在做一些事情,我意识到我想要在一个字符串中找到多少个/s,然后我突然想到,有几种方法可以做到这一点,但不能决定哪种是最好的(或最简单的)。
目前我想说的是:
string source = "/once/upon/a/time/";
int count = source.Length - source.Replace("/", "").Length;
但我一点都不喜欢,有人愿意吗?
我并不想为此挖掘出正则表达式,对吧?
我知道我的字符串将包含我要搜索的项,所以你可以假设…
当然对于长度为> 1的字符串,
string haystack = "/once/upon/a/time";
string needle = "/";
int needleCount = ( haystack.Length - haystack.Replace(needle,"").Length ) / needle.Length;
我最初的想法是这样的:
public static int CountOccurrences(string original, string substring)
{
if (string.IsNullOrEmpty(substring))
return 0;
if (substring.Length == 1)
return CountOccurrences(original, substring[0]);
if (string.IsNullOrEmpty(original) ||
substring.Length > original.Length)
return 0;
int substringCount = 0;
for (int charIndex = 0; charIndex < original.Length; charIndex++)
{
for (int subCharIndex = 0, secondaryCharIndex = charIndex; subCharIndex < substring.Length && secondaryCharIndex < original.Length; subCharIndex++, secondaryCharIndex++)
{
if (substring[subCharIndex] != original[secondaryCharIndex])
goto continueOuter;
}
if (charIndex + substring.Length > original.Length)
break;
charIndex += substring.Length - 1;
substringCount++;
continueOuter:
;
}
return substringCount;
}
public static int CountOccurrences(string original, char @char)
{
if (string.IsNullOrEmpty(original))
return 0;
int substringCount = 0;
for (int charIndex = 0; charIndex < original.Length; charIndex++)
if (@char == original[charIndex])
substringCount++;
return substringCount;
}
使用替换和除法的大海捞针方法产生21秒以上,而这需要大约15.2秒。
在添加位后进行编辑,这将添加子字符串。长度- 1到charIndex(就像它应该的那样),它在11.6秒。
编辑2:我使用了一个有26个双字符字符串的字符串,这里是更新到相同示例文本的时间:
大海捞针(OP版本):7.8秒
建议的机制:4.6秒。
编辑3:添加单个字符的大小写,它变成了1.2秒。
编辑4:作为上下文:使用了5000万次迭代。
如果你看看这个网页,有15种不同的方法进行了基准测试,包括使用并行循环。
最快的方法似乎是使用单线程for循环(如果您的。net版本< 4.0)或并行。for循环(如果使用。net > 4.0进行数千次检查)。
假设“ss”是你的搜索字符串,“ch”是你的字符数组(如果你有一个以上的字符你正在寻找),下面是代码的基本要点,有最快的运行时间单线程:
for (int x = 0; x < ss.Length; x++)
{
for (int y = 0; y < ch.Length; y++)
{
for (int a = 0; a < ss[x].Length; a++ )
{
if (ss[x][a] == ch[y])
//it's found. DO what you need to here.
}
}
}
还提供了基准测试源代码,以便您可以运行自己的测试。
string source = "/once/upon/a/time/";
int count = 0, n = 0;
while ((n = source.IndexOf('/', n) + 1) != 0) count++;
这是Richard Watson的答案的一个变体,char在字符串中出现的次数越多,效率就会提高一点,代码也会更少!
虽然我必须说,在没有广泛测试每个场景的情况下,我确实看到了使用以下方法的显著速度提升:
int count = 0;
for (int n = 0; n < source.Length; n++) if (source[n] == '/') count++;
查找字符计数与查找字符串计数有很大不同。另外,这也取决于你是否想要检查不止一个。如果你想检查各种不同的字符计数,像这样的东西可以工作:
var charCounts =
haystack
.GroupBy(c => c)
.ToDictionary(g => g.Key, g => g.Count());
var needleCount = charCounts.ContainsKey(needle) ? charCounts[needle] : 0;
注1:分组到字典中非常有用,因此为它编写GroupToDictionary扩展方法非常有意义。
注意2:拥有自己的字典实现也很有用,它允许默认值,然后您可以自动为不存在的键获取0。