【发布时间】:2011-07-08 19:27:42
【问题描述】:
计算字符串中特定字符出现次数的最简单方法是什么?
也就是说,我需要写一个函数countTheCharacters(),这样
str = "the little red hen"
count = countTheCharacters(str,"e") ' Count should equal 4
count = countTheCharacters(str,"t") ' Count should equal 3
【问题讨论】:
-
最快的方法是不使用字符串。如果你真的对速度感兴趣,你应该寻找其他东西。
-
@habakuk,OP 可以使用什么非字符串值来代替“小红母鸡”?
-
@johnywhy 为什么要使用 StringBuilder。见msdn.microsoft.com/de-de/library/…