如何计算特定字符串在另一个字符串中出现的次数。例如,这就是我试图在Javascript中做的事情:

var temp = "This is a string.";
alert(temp.count("is")); //should output '2'

当前回答

       var myString = "This is a string.";
        var foundAtPosition = 0;
        var Count = 0;
        while (foundAtPosition != -1)
        {
            foundAtPosition = myString.indexOf("is",foundAtPosition);
            if (foundAtPosition != -1)
            {
                Count++;
                foundAtPosition++;
            }
        }
        document.write("There are " + Count + " occurrences of the word IS");

请参阅:-count字符串中出现的子字符串,以了解分步说明。

其他回答


subst_count从php转换为Javascript


Locutus(将Php翻译为JS的包)subst_count(官方页面,代码复制如下)

function substr_count (haystack, needle, offset, length) { 
  // eslint-disable-line camelcase
  //  discuss at: https://locutus.io/php/substr_count/
  // original by: Kevin van Zonneveld (https://kvz.io)
  // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
  // improved by: Brett Zamir (https://brett-zamir.me)
  // improved by: Thomas
  //   example 1: substr_count('Kevin van Zonneveld', 'e')
  //   returns 1: 3
  //   example 2: substr_count('Kevin van Zonneveld', 'K', 1)
  //   returns 2: 0
  //   example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10)
  //   returns 3: false

  var cnt = 0

  haystack += ''
  needle += ''
  if (isNaN(offset)) {
    offset = 0
  }
  if (isNaN(length)) {
    length = 0
  }
  if (needle.length === 0) {
    return false
  }
  offset--

  while ((offset = haystack.indexOf(needle, offset + 1)) !== -1) {
    if (length > 0 && (offset + needle.length) > length) {
      return false
    }
    cnt++
  }

  return cnt
}

查看Locutus对Php的subst_count函数的翻译

此函数在三种模式下工作:查找字符串中单个字符的频率,查找字符串中相邻子字符串的频率,然后如果它与一个匹配,则会直接向前移动到它后面的下一个,第三个与前一个相似,但它也会计算给定字符串中的交叉子字符串

函数substringFrequency(字符串、子字符串、连接){let索引允许发生频率=0for(设i=0;i<string.length;i++){index=string.indexOf(substring,i)如果(索引!=-1){if((子字符串长度==1)||连接==true){i=索引}其他{i=索引+1}发生频率++}其他{打破} }return(发生频率)}console.log(substringFrequency('vvv','v'))console.log(substringFrequency('vvv','vv'))console.log(substringFrequency('vvv','vv'))

没有人会看到这一点,但偶尔带回递归和箭头函数是很好的(双关语的意思很好)

String.prototype.occurrencesOf = function(s, i) {
 return (n => (n === -1) ? 0 : 1 + this.occurrencesOf(s, n + 1))(this.indexOf(s, (i || 0)));
};

参数: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”));

无正则表达式的简单版本:

var temp=“这是一个字符串。”;var计数=(临时拆分('is').长度-1);警报(计数);