character count in js code example
Example 1: js character certain count
var str = "Hello Lewes";
var ch = 'e';
var count = str.split("e").length - 1;
console.log(count);
Example 2: js string includes count
function occurrences(string, subString, allowOverlapping) {
string += "";
subString += "";
if (subString.length <= 0) return (string.length + 1);
var n = 0,
pos = 0,
step = allowOverlapping ? 1 : subString.length;
while (true) {
pos = string.indexOf(subString, pos);
if (pos >= 0) {
++n;
pos += step;
} else break;
}
return n;
}
Example 3: javascript count character in string
var str = "Hello World!";
var n = str.length;