js统计给定字符串在另一个字符串中出现的次数
文章标签
JavaScript
我给大家总结了几种方法,希望能帮助大家理解和学习。
最好的方法
var childInNums = Parent.split(child).length - 1;
使用火柴到达
函数 strCount(str, 目标) {
让计数 = 0
if (!target) 返回数量
while(str.match(目标)) {
str = str.replace(目标, '')
计数++
}
返回计数
}
console.log(strCount('abcdef abcdef a', 'abc'))
使用indexOf到达
函数 substrCount(str, 目标) {
让计数= 0;
while (str.includes(目标)) {
const 索引 = str.indexOf(目标);
计数++;
str = str.substring(index + target.length);
}
返回计数;
}
以上是我的总结。希望能够帮助大家理解、学习、拓展自己的想法。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
上一篇:定期实施验证中文 下一篇:JS去除制表符和换行符
code前端网

发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。