Count the Number of Occurrences of a Character in a String in JavaScript

Count the number of occurrences of a character in a string in Javascript

I have updated this answer. I like the idea of using a match better, but it is slower:

console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3

console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4

Count number of occurrences for each char in a string

This is really, really simple in JavaScript (or any other language that supports maps):

// The string
var str = "I want to count the number of occurrences of each char in this string";

// A map (in JavaScript, an object) for the character=>count mappings
var counts = {};

// Misc vars
var ch, index, len, count;

// Loop through the string...
for (index = 0, len = str.length; index < len; ++index) {
// Get this character
ch = str.charAt(index); // Not all engines support [] on strings

// Get the count for it, if we have one; we'll get `undefined` if we
// don't know this character yet
count = counts[ch];

// If we have one, store that count plus one; if not, store one
// We can rely on `count` being falsey if we haven't seen it before,
// because we never store falsey numbers in the `counts` object.
counts[ch] = count ? count + 1 : 1;
}

Now counts has properties for each character; the value of each property is the count. You can output those like this:

for (ch in counts) {
console.log(ch + " count: " + counts[ch]);
}

How to count string occurrence in string?

The g in the regular expression (short for global) says to search the whole string rather than just find the first occurrence. This matches is twice:

var temp = "This is a string.";

var count = (temp.match(/is/g) || []).length;

console.log(count);

Count the number of occurrences of a character in a string

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4

counting the number of times a character is used in a string for all of a strings characters in Javascript

Just loop through every character of the string, and count them in an object.

A simple solution would be:

let str = "The quick brown fox jumps over the lazy dog";

const count = string => {

const characters = {};

for (let character of string) {

characters[character] = characters[character] + 1 || 1;

}

return characters;

}

console.log(count(str));

ES6 / lodash count the number of occurrences of a character in a string

And here's a lodash solution:

const count = (str, ch) => _.countBy(str)[ch] || 0;

console.log(count("abcadea", "a"));
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

Get number of occurrences of a character in JavaScript

If you need to check the occurances of a simple pattern as "," then better don't use regular expressions.

Try:

var matchesCount = string.split(",").length - 1;

Count the number of occurrences of a character in a string in Javascript

I have updated this answer. I like the idea of using a match better, but it is slower:

console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3

console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4


Related Topics



Leave a reply



Submit