You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let str = 's'
let ostr = 'seowqjoewqskodkwqkdpsadpdkpwq'
let index = 0
ostr.split('').forEach(item => {
if (item === str){
index++
}
})
console.log(index);
Activity
linghucq1 commentedon May 8, 2019
coderfee commentedon May 9, 2019
undefinedYu commentedon May 10, 2019
这里找到一个好方法。希望能有用。
function substrCount(str, target) {
if(Object.prototype.toString.call(str).slice(8,-1) === 'String' && !str)
alert("请填写字符串");
else
return (str.match(new RegExp(target, 'g')).length);
}
qqdnnd commentedon May 16, 2019
Shoryukenbt commentedon May 28, 2019
var childInNums = parent.split(child).length - 1, 这个没毛病吧
tiyunchen commentedon May 31, 2019
感觉没毛病
myprelude commentedon Jun 13, 2019
AricZhu commentedon Jun 19, 2019
// 递归,一行代码实现
function strRepeatCount (subStr, str, count=0) {
}
bWhirring commentedon Jul 3, 2019
Konata9 commentedon Jul 4, 2019
poporeki commentedon Jul 4, 2019
这个好啊 又简单
15190408121 commentedon Jul 5, 2019
function strFind (str, target) {
var lengths = 0
if (!target) {
return lengths
}
while(str.match(target)) {
str = str.replace(target, '')
lengths++
}
return lengths
}
var str = "你好 萨达所大所多所多所问问二位无 你好 萨达所大所多 你好"
strFind(str, "你好")
taiyangxingchen commentedon Jul 10, 2019
这个真心不错
shufangyi commentedon Jul 18, 2019
63 remaining items
Rednaxela-2 commentedon Jan 26, 2022
我试了几个方法,这个的速度也是最快的
github-cxtan commentedon Feb 15, 2022
function GetCounts(srcData, DetectStr){
let reg = new RegExp(
${DetectStr}
, 'g');return srcData.match(reg).length;
}
QiaoIsMySky commentedon Feb 21, 2022
let str = 's'
let ostr = 'seowqjoewqskodkwqkdpsadpdkpwq'
let index = 0
ostr.split('').forEach(item => {
if (item === str){
index++
}
})
console.log(index);
sup194 commentedon Feb 23, 2022
function strCount(str, target) {
let count = 0
if (!target) return count
while (str.match(target)) {
str.replact('')
count++
}
return count
}
yxllovewq commentedon Mar 8, 2022
不使用正则:
使用正则:
storm-cao commentedon Mar 11, 2022
const countString = (childStr,parentStr) => parentStr.split(childStr).length - 1);
wenxd commentedon May 20, 2022
xiaoqiangz commentedon May 22, 2022
// 统计某一字符或字符串在另一个字符串中出现的次数
function repatCount(str, target) {
// 第一种
// return str.split(target).length - 1
// 第二种
// let count = 0
// while (str.includes(target)) {
// count++
// str = str.replace(target, '')
// }
// return count
// 第三种
let reg = new RegExp(target, 'g')
return str.match(reg).length
}
console.log(repatCount('abcdeaveavffavvfa', 'av'))
xiaoxiaozhiya commentedon May 28, 2022
function countStr(str1, str2) { let count = 0; while (str1 && str2 && str2.includes(str1)) { let index = str2.indexOf(str1); count++; str2 = str2.substring(index + str1.length); console.log(str2) } return count; }
www-wanglong commentedon Jun 18, 2022
function count(str, target) {
let reg = new RegExp(str, 'g')
return target.match(reg).length
}
count('a', 'abcsadfwretaabcerwawabc')
Sobrium commentedon Sep 8, 2022
function countStr(str, target){
var count = 0;
while(str.includes(target)){
count++;
str = str.replace(target, '');
}
}
mocc124 commentedon Dec 4, 2022
tcxiao1 commentedon Aug 10, 2023
function countStr(target, array, rightIndex, leftIndex, count) {
}
let target = "aa";
let src = "aaabbcaacddaabbccdd";
let countStr1 = countStr(target, Array.from(src), 0, target.length, 0);
console.log(countStr1)
双指针
lili-0923 commentedon Feb 2, 2024
function countStr(str1, str2) {
return str2.split(str1).length - 1;
}
pengsir120 commentedon Sep 8, 2024
function count(str1, str2) {
if(typeof str1 !== 'string' && typeof str2 !== 'string') {
return
}
let pos = count = 0
while(str2.indexOf(str1, pos) > -1) {
pos = str2.indexOf(str1, pos) + str1.length
count++
}
return count
}