Answers for "how to count how many times a world appears in string"

1

python count how many times a word appears in a string

# credit to Stack Overflow user in source link

sentence.lower().split().count(word)
Posted by: Guest on May-28-2021
0

function to check how many times a character appears in a string

function letterCount(string, letter, caseSensitive) {
    var count = 0;
    if ( !caseSensitive) {
        string = string.toUpperCase();
        letter = letter.toUpperCase();
    }
    for (var i=0, l=string.length; i<string.length; i += 1) {
        if (string[i] === letter) {
            count += 1;
        }
    }
    return count;
}

var exampleString = 'I will practice survival skills';
console.log(letterCount(exampleString, 'i', true)); // 4 (doesn't match uppercase I at beginning)
console.log(letterCount(exampleString, 'i', false)); 5 (matches both upper and lowercase i)
Posted by: Guest on June-29-2021

Code answers related to "how to count how many times a world appears in string"

Python Answers by Framework

Browse Popular Code Answers by Language