check how many times a substring appears in a string

Solutions on MaxInterview for check how many times a substring appears in a string by the best coders in the world

showing results for - "check how many times a substring appears in a string"
Benji
20 Jun 2020
1a = '128532012'
2print(a.count('0'))
3#Prints: 1
4
5print(a.count('12'))
6#Prints: 2
7
Marta
20 May 2018
1function letterCount(string, letter, caseSensitive) {
2    var count = 0;
3    if ( !caseSensitive) {
4        string = string.toUpperCase();
5        letter = letter.toUpperCase();
6    }
7    for (var i=0, l=string.length; i<string.length; i += 1) {
8        if (string[i] === letter) {
9            count += 1;
10        }
11    }
12    return count;
13}
14
15var exampleString = 'I will practice survival skills';
16console.log(letterCount(exampleString, 'i', true)); // 4 (doesn't match uppercase I at beginning)
17console.log(letterCount(exampleString, 'i', false)); 5 (matches both upper and lowercase i)
18
queries leading to this page
how to count the number of times a substring appears in a stringhow many times a substring contains in a stringfind how many times a substring occurs in a string javafuntion to count the number of times a letter appears in a stringcount how many times a string appears in another string pythonhow to check how many times a substring appears in a stringcheck how many times a substring appears in a string javahow to write a function that counts how many times a substring occurs in a strringnumber of times a substring appears in a stringhow many times a character occurs in a string 3fhow to find number of times a substring is present in a stringcheck how many times a substring appears in a stringfind how many times a substring occurs in a stringcheck the number of times a character occurs in a stringhow to find how many times a character is in stringalgorithm that determine if a number or character appear many times in a stringfind how many times a letter is in a stringwrite a function that counts how many times a substring occurs in a string count the number of times a character appears in a stringhow many times substring in string pythoncount how many times a character appears in a stringprint the number of times the string strfind appears in the stringscheck the number of times the appears in the given stringcheck how many times a substring appears in a string java c2 a8python count number of times substring appears in stringsee how many times a string is presentfind how many times a char appears in a substringhow to check how many substrings sre in a stringgiven a string and character 2c determine how many times that character appears in the stringcount number of times substring appears in stringpython how many times string in stringfind number of times a string occurs as a substring in given stringcheck how many times a string has characterpython check how many times substring in stringcheck how many times a substring appears in a string