In js, determine the number of times a character appears in a string, and what are the methods?

find the number of times the letter e appears:

let str = "hello everyone";
< hr >

if so, the methods I can think of at present are:

  1. use the split method let num = str.split ("e"). Length-1; num = > 4 )
  2. use the match method (global search) let reg = / str.match (reg) g; let num = str.match (reg) .length; num = > 4

is there any other way?

Mar.03,2021

let str='hello everyone';
function catStr(str){
    ler num=0;
    for(var i=0,len=str.length;i<len;iPP){
        if(str[i]=='e'){
            numPP
        }
    }
return num
}

catStr(str)

for is the lowest performance


var strs = "hello everyone"
var num = strs.split ('') .filter (I = > I = ='e'). Length
/ / 4


Super fast!

console.time('start');
let num = 0;
let str = "hello everyone"
while(str.indexOf('e')!== -1) {
    str = str.slice(str.indexOf('e') + 1)
    num += 1
}
console.timeEnd('start')
start: 0.024658203125ms
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb4db5-250a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb4db5-250a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?