check how many times a substring appears in a string
a = '128532012'
print(a.count('0'))
#Prints: 1
print(a.count('12'))
#Prints: 2
check how many times a substring appears in a string
a = '128532012'
print(a.count('0'))
#Prints: 1
print(a.count('12'))
#Prints: 2
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)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us