如何计算特定字符串在另一个字符串中出现的次数。例如,这就是我试图在Javascript中做的事情:
var temp = "This is a string.";
alert(temp.count("is")); //should output '2'
如何计算特定字符串在另一个字符串中出现的次数。例如,这就是我试图在Javascript中做的事情:
var temp = "This is a string.";
alert(temp.count("is")); //should output '2'
当前回答
试试看:
function countString(str, search){
var count=0;
var index=str.indexOf(search);
while(index!=-1){
count++;
index=str.indexOf(search,index+1);
}
return count;
}
其他回答
ES2020提供了一个新的MatchAll,它可能在这个特定的环境中使用。
这里我们创建了一个新的RegExp,请确保将“g”传递到函数中。
使用Array.from转换结果并计算长度,根据原始请求者所需的输出返回2。
let strToCheck=RegExp('is','g')let matchesReg=“这是一个字符串。”.matchAll(strToCheck)console.log(Array.from(matchesReg).length)//2
参数:ustring:超集字符串countChar:子字符串
一个计算JavaScript中子字符串出现次数的函数:
函数subStringCount(ustring,countChar){var correspCount=0;var corresp=false;变量量=0;var prevChar=空;对于(var i=0;i!=ustring.length;i++){如果(ustring.charAt(i)==countChar.charAt(0)&&corresp==false){corresp=真;correspCount+=1;如果(correspCount==countChar.length){数量+=1;corresp=false;correspCount=0;}prevChar=1;}否则如果(ustring.charAt(i)==countChar.charAt(prevChar)&&corresp==true){correspCount+=1;如果(correspCount==countChar.length){数量+=1;corresp=false;correspCount=0;prevChar=空;}其他{prevChar+=1;}}其他{corresp=false;correspCount=0;}} 回报金额;}console.log(subStringCount(“Hello World,Hello World”,“ll”));
这是最快的功能!
为什么速度更快?
不逐个字符检查(有1个例外)使用while并增加1个var(字符计数var),而不是for循环检查长度并增加2个var(通常是var i和一个带有字符计数的var)使用WAY less vars不使用正则表达式!使用(希望)高度优化的函数所有操作都尽可能地组合在一起,避免了由于多次操作而导致的速度减慢String.product.timesCharExist=函数(c){var t=0,l=0,c=(c+“”)[0];while(l=this.indexOf(c,l)+1)++t;return t};
以下是一个更慢、更可读的版本:
String.prototype.timesCharExist = function ( chr ) {
var total = 0, last_location = 0, single_char = ( chr + '' )[0];
while( last_location = this.indexOf( single_char, last_location ) + 1 )
{
total = total + 1;
}
return total;
};
由于计数器、长的var名称和对1var的误用,这个速度较慢。
要使用它,只需执行以下操作:
'The char "a" only shows up twice'.timesCharExist('a');
编辑:(2013/12/16)
不要与Opera 12.16或更高版本一起使用!它将比正则表达式解决方案花费几乎2.5倍的时间!
在chrome上,对于1000000个字符,此解决方案需要14ms到20ms。
相同量的regex溶液需要11-14ms。
使用函数(String.prototype外部)大约需要10-13ms。
以下是使用的代码:
String.prototype.timesCharExist=function(c){var t=0,l=0,c=(c+'')[0];while(l=this.indexOf(c,l)+1)++t;return t};
var x=Array(100001).join('1234567890');
console.time('proto');x.timesCharExist('1');console.timeEnd('proto');
console.time('regex');x.match(/1/g).length;console.timeEnd('regex');
var timesCharExist=function(x,c){var t=0,l=0,c=(c+'')[0];while(l=x.indexOf(c,l)+1)++t;return t;};
console.time('func');timesCharExist(x,'1');console.timeEnd('func');
所有解决方案的结果应该是100000!
注意:如果您希望此函数计数超过1个字符,请将其中的c=(c+“”)[0]更改为c=c+“”
函数countInstance(字符串,单词){返回字符串.split(word).length-1;}console.log(countInstance(“This is a string”,“is”))
var countInstances=函数(主体,目标){var全局计数器=0;var concatstring=“”;for(var i=0,j=target.length;i<body.length;i++){concatstring=body.substring(i-1,j);if(concatstring===目标){全局计数器+=1;concatstring='';}}返回全局计数器;};console.log(countInstance('abcabc','abc'));//==>2.console.log(countInstance('ababa','aba'));//==>2.console.log(countInstance('aaabbb','ab'));//==>1.