通过 split() 函数
var charCount = function(s, ch) {
return s.split(ch).length - 1;
}
str = 'hello world';
ch = 'o';
console.log(charCount(str, ch)); // 2
版权声明:本文为m0_52575541原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
var charCount = function(s, ch) {
return s.split(ch).length - 1;
}
str = 'hello world';
ch = 'o';
console.log(charCount(str, ch)); // 2