Quantcast
Viewing latest article 6
Browse Latest Browse All 43

Answer by polendina for How to count string occurrence in string?

This function works in three modes: looking for the frequency of a single character within a string , a contiguous substring within a string then if it does match one it moves right ahead to next one right after it , and the third is similar to the previous one except it it will also count intersecting substrings within the given string

function substringFrequency(string , substring , conjunction){    let index     let occurenceFrequency  = 0    for (let i=0 ; i < string.length  ; i++){        index = string.indexOf(substring , i)        if (index != -1){            if ((substring.length == 1 ) || conjunction == true) {                i = index             }else {                i = index + 1            }            occurenceFrequency++        }else{            break        }     }    return (occurenceFrequency)}console.log(substringFrequency('vvvv' , 'v' ))console.log(substringFrequency('vvvv' , 'vv'))console.log(substringFrequency('vvvv' , 'vv' , true))

Viewing latest article 6
Browse Latest Browse All 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>