我需要计算字符串中某个字符出现的次数。

例如,假设我的字符串包含:

var mainStr = "str1,str2,str3,str4";

我想求出逗号的个数,也就是3个字符。以及按逗号分隔后的单个字符串的计数,也就是4。

我还需要验证每个字符串,即str1或str2或str3或str4不应该超过,比如说,15个字符。


当前回答

String.prototype.reduce = Array.prototype.reduce; String.prototype.count =函数(c) { 返回。Reduce (((n, x) => n + (x === c ?)1: 0)), 0) }; Const n = "bugs bunny was here".count("b") console.log (n)

类似于基于上面的原型,但没有为字符串分配数组。分配几乎是上面每个版本的问题,除了循环变量。这避免了循环代码,重用了浏览器实现的数组。reduce函数。

其他回答

我的解决方案:

function countOcurrences(str, value){
   var regExp = new RegExp(value, "gi");
   return str.match(regExp) ? str.match(regExp).length : 0;  
}
s = 'dir/dir/dir/dir/'
for(i=l=0;i<s.length;i++)
if(s[i] == '/')
l++

更新06/10/2022

所以我运行了各种性能测试,如果你的用例允许的话,使用split似乎会表现得最好。


function countChar(char: string, string: string): number  {

  return string.split(char).length - 1

}

countChar('x', 'foo x bar x baz x')


我知道我来晚了,但我很困惑,没有人用最基本的方法来回答这个问题。社区对这个问题提供的大部分答案都是基于迭代的,但都是在每个字符的基础上移动字符串,这并不是真正有效的。

When dealing with a large string that contains thousands of characters walking over each character to get the occurance count can become rather extraneous not to mention a code-smell. The below solutions take advantage of slice, indexOf and the trusted traditional while loop. These approaches prevent us having to walk over each character and will greatly speed up the time it takes to count occurances. These follow similar logic to that you'd find in parsers and lexical analyzers that require string walks.

与Slice一起使用

在这种方法中,我们利用切片和每个indexOf匹配,我们将通过字符串移动我们的方式,并消除之前搜索的药水。每次调用indexOf,它搜索的字符串的大小都会变小。

function countChar (char: string, search: string): number {
  
  let num: number = 0;
  let str: string = search;
  let pos: number = str.indexOf(char);
  
  while(pos > -1) {
    str = str.slice(pos + 1);
    pos = str.indexOf(char);
    num++;
  }

  return num;

}

// Call the function
countChar('x', 'foo x bar x baz x') // 3

使用IndexOf from position

类似于使用slice的第一种方法,但它不是扩大我们正在搜索的字符串,而是利用indexOf方法中的from参数。

function countChar (char: string, str: string): number {
  
  let num: number = 0;
  let pos: number = str.indexOf(char);
  
  while(pos > -1) {
    pos = str.indexOf(char, pos + 1);
    num++;
  }

  return num;

}

// Call the function
countChar('x', 'foo x bar x baz x') // 3

就我个人而言,我倾向于第二种方法而不是第一种,但在处理大字符串和较小尺寸的字符串时,两者都很好且性能良好。

我用ramda js的解决方案:

const testString = 'somestringtotest'

const countLetters = R.compose(
  R.map(R.length),
  R.groupBy(R.identity),
  R.split('')
)

countLetters(testString)

链接到REPL。

至少有五种方法。最好的选项,也应该是最快的(由于本机RegEx引擎)被放在顶部。

方法1

("this is foo bar".match(/o/g)||[]).length;
// returns 2

方法2

"this is foo bar".split("o").length - 1;
// returns 2

不建议拆分,因为它是资源饥渴的。它为每个匹配分配新的“Array”实例。不要通过FileReader尝试>100MB文件。你可以观察确切的资源使用使用Chrome的分析器选项。

方法3

    var stringsearch = "o"
       ,str = "this is foo bar";
    for(var count=-1,index=-2; index != -1; count++,index=str.indexOf(stringsearch,index+1) );
// returns 2

方法4

搜索单个字符

    var stringsearch = "o"
       ,str = "this is foo bar";
    for(var i=count=0; i<str.length; count+=+(stringsearch===str[i++]));
     // returns 2

方法5

元素映射和过滤。不建议这样做,因为它的整体资源预分配,而不是使用python的“生成器”:

    var str = "this is foo bar"
    str.split('').map( function(e,i){ if(e === 'o') return i;} )
                 .filter(Boolean)
    //>[9, 10]
    [9, 10].length
    // returns 2

分享: 我做了这个要点,目前有8种方法的字符计数,所以我们可以直接汇集和分享我们的想法-只是为了好玩,也许一些有趣的基准:)