Skip to content

[js] 第7天 统计某一字符或字符串在另一个字符串中出现的次数 #21

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第7天 统计某一字符或字符串在另一个字符串中出现的次数

Activity

linghucq1

linghucq1 commented on May 8, 2019

@linghucq1
function strCount(str, target) {
  let count = 0
  if (!target) return count
  while(str.match(target)) {
    str = str.replace(target, '')
    count++
  }
  return count
}

console.log(strCount('abcdef abcdef a', 'abc'))
coderfee

coderfee commented on May 9, 2019

@coderfee
function substrCount(str, target) {
  let count = 0;
  while (str.includes(target)) {
    const index = str.indexOf(target);
    count++;
    str = str.substring(index + target.length);
  }
  return count;
}
undefinedYu

undefinedYu commented on May 10, 2019

@undefinedYu
Contributor

这里找到一个好方法。希望能有用。
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

qqdnnd commented on May 16, 2019

@qqdnnd
function getStrTimes(str,rule){
    return rule !=='' && str.match(new RegExp(rule,'g'))? str.match(new RegExp(rule,'g')).length : 0;
}
Shoryukenbt

Shoryukenbt commented on May 28, 2019

@Shoryukenbt

var childInNums = parent.split(child).length - 1, 这个没毛病吧

tiyunchen

tiyunchen commented on May 31, 2019

@tiyunchen

var childInNums = parent.split(child).length - 1, 这个没毛病吧

感觉没毛病

myprelude

myprelude commented on Jun 13, 2019

@myprelude
fucntion repeat(str,parentStr){
  return parentStr.split(str).length - 1
}
AricZhu

AricZhu commented on Jun 19, 2019

@AricZhu

// 递归,一行代码实现
function strRepeatCount (subStr, str, count=0) {

return str.indexOf(subStr) !== -1? strRepeatCount(subStr, str.substring(str.indexOf(subStr)+subStr.length), ++count): count;

}

bWhirring

bWhirring commented on Jul 3, 2019

@bWhirring
function count(str, param) {
  const reg = new RegExp(param, 'g');
  return str.match(reg).length;
}
Konata9

Konata9 commented on Jul 4, 2019

@Konata9
const countAppears = (str, target) => {
  let count = 0;

  if (!str || !target) {
    return count;
  }

  const keyIndex = target.indexOf(str);
  if (keyIndex > -1) {
    count = 1 + countAppears(str, target.slice(keyIndex + 1));
  }

  return count;
};

const str = "abcaaadefg2333333333334abcddddea";

console.log(countAppears("2", str));
console.log(countAppears("b", str));
console.log(countAppears("d", str));
console.log(countAppears("a", str));
console.log(countAppears("f", str));
poporeki

poporeki commented on Jul 4, 2019

@poporeki

var childInNums = parent.split(child).length - 1, 这个没毛病吧

这个好啊 又简单

15190408121

15190408121 commented on Jul 5, 2019

@15190408121

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

taiyangxingchen commented on Jul 10, 2019

@taiyangxingchen

var childInNums = parent.split(child).length - 1, 这个没毛病吧

这个真心不错

shufangyi

shufangyi commented on Jul 18, 2019

@shufangyi
const countString = (str, chars) => (
  (arr = str.match(new RegExp(chars, 'g'))), arr && chars ? arr.length : 0
)

63 remaining items

Rednaxela-2

Rednaxela-2 commented on Jan 26, 2022

@Rednaxela-2

var childInNums = parent.split(child).length - 1, 这个没毛病吧

我试了几个方法,这个的速度也是最快的

github-cxtan

github-cxtan commented on Feb 15, 2022

@github-cxtan

function GetCounts(srcData, DetectStr){
let reg = new RegExp(${DetectStr}, 'g');
return srcData.match(reg).length;
}

QiaoIsMySky

QiaoIsMySky commented on Feb 21, 2022

@QiaoIsMySky

let str = 's'
let ostr = 'seowqjoewqskodkwqkdpsadpdkpwq'
let index = 0
ostr.split('').forEach(item => {
if (item === str){
index++
}
})
console.log(index);

sup194

sup194 commented on Feb 23, 2022

@sup194

function strCount(str, target) {
let count = 0
if (!target) return count
while (str.match(target)) {
str.replact('')
count++
}
return count
}

yxllovewq

yxllovewq commented on Mar 8, 2022

@yxllovewq

不使用正则:

const strCountOf = (str = '', fromStr = '') => {
  let count = 0;
  let start = false;
  let startId;
  for (let i = 0; i < fromStr.length; i++) {
    if (str[0] === fromStr[i]) {
      start = true;
      startId = i;
    }
    if (start) {
      if (str[i - startId] !== fromStr[i]) {
        start = false;
      }
      if (i === startId + str.length - 1) {
        count++;
        start = false;
      }
    }
  }
  return count;
}

使用正则:

const strCountOf = (str = '', fromStr = '') => [...fromStr.matchAll(new RegExp(str, 'g'))].length;
storm-cao

storm-cao commented on Mar 11, 2022

@storm-cao

const countString = (childStr,parentStr) => parentStr.split(childStr).length - 1);

wenxd

wenxd commented on May 20, 2022

@wenxd
function statCharInString(str, char) {
    let re = new RegExp(char, 'g')
    let num = str.match(re).length
    console.log(num);
  }
  statCharInString('asf sfas  sadf jjsfahf', 'as')
xiaoqiangz

xiaoqiangz commented on May 22, 2022

@xiaoqiangz

// 统计某一字符或字符串在另一个字符串中出现的次数
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

xiaoxiaozhiya commented on May 28, 2022

@xiaoxiaozhiya

方法一正则
function getTotalCount(str,target){ var reg=new RegExp(target,"g") var arr=str.match(reg) return arr.length }

方法二
利用字符串的位置关系(间接)
function countStr(str1, str2) { var arr=str2.split(str1) console.log(arr) return str2.split(str1).length - 1; }

方法三
一边判断一边丢

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; }

  • substring 方法不接受负的参数
www-wanglong

www-wanglong commented on Jun 18, 2022

@www-wanglong

function count(str, target) {
let reg = new RegExp(str, 'g')
return target.match(reg).length
}

count('a', 'abcsadfwretaabcerwawabc')

Sobrium

Sobrium commented on Sep 8, 2022

@Sobrium

function countStr(str, target){
var count = 0;
while(str.includes(target)){
count++;
str = str.replace(target, '');
}
}

mocc124

mocc124 commented on Dec 4, 2022

@mocc124
function getCount(str, char) {
  let index = str.indexOf(char);
  if (index < 0) return 0;
  return 1 + getCount(str.slice(index + char.length), char);
}
tcxiao1

tcxiao1 commented on Aug 10, 2023

@tcxiao1

function countStr(target, array, rightIndex, leftIndex, count) {

if (leftIndex > array.length) {
    return count;
}

let source = "";
for (let i = rightIndex; i < leftIndex; i++) {
    source = source + array[i];
}
if (source === target) {
    count++;
}

rightIndex++;
leftIndex++;
return 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

lili-0923 commented on Feb 2, 2024

@lili-0923

function countStr(str1, str2) {
return str2.split(str1).length - 1;
}

pengsir120

pengsir120 commented on Sep 8, 2024

@pengsir120

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mopig@haizhilin2013@gaoryrt@JJL-SH@Konata9

        Issue actions

          [js] 第7天 统计某一字符或字符串在另一个字符串中出现的次数 · Issue #21 · haizlin/fe-interview