Quantcast
Channel: How to count string occurrence in string? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by Force Bolt for How to count string occurrence in string?

$
0
0
//Try this codeconst countSubStr = (str, search) => {    let arrStr = str.split('');    let i = 0, count = 0;    while(i < arrStr.length){        let subStr = i + search.length + 1 <= arrStr.length ?                  arrStr.slice(i, i+search.length).join('') :                  arrStr.slice(i).join('');        if(subStr === search){            count++;            arrStr.splice(i, search.length);        }else{            i++;        }    }    return count;  }

Viewing all articles
Browse latest Browse all 43

Trending Articles