Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to count the number of occurrences of a character in a string in JavaScript?
There are several methods to count character occurrences in a JavaScript string. Here are the most effective approaches:
Using a For Loop
The traditional method uses a simple for loop to iterate through each character:
<!DOCTYPE html>
<html>
<body>
<script>
function countCharacter(str, char) {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
return count;
}
let text = "hello world";
let character = "l";
let result = countCharacter(text, character);
document.write("Character '" + character + "' appears " + result + " times in '" + text + "'");
</script>
</body>
</html>
Character 'l' appears 3 times in 'hello world'
Using split() Method
Split the string by the target character and count the resulting parts:
<!DOCTYPE html>
<html>
<body>
<script>
function countWithSplit(str, char) {
return str.split(char).length - 1;
}
let text = "javascript programming";
let character = "a";
let result = countWithSplit(text, character);
document.write("Character '" + character + "' appears " + result + " times in '" + text + "'");
</script>
</body>
</html>
Character 'a' appears 2 times in 'javascript programming'
Using Regular Expression
Use regex with the global flag to find all matches:
<!DOCTYPE html>
<html>
<body>
<script>
function countWithRegex(str, char) {
let regex = new RegExp(char, 'g');
let matches = str.match(regex);
return matches ? matches.length : 0;
}
let text = "programming";
let character = "m";
let result = countWithRegex(text, character);
document.write("Character '" + character + "' appears " + result + " times in '" + text + "'");
</script>
</body>
</html>
Character 'm' appears 2 times in 'programming'
Interactive Example
Here's a complete interactive example:
<!DOCTYPE html>
<html>
<body>
<script>
function displayCount() {
let str = document.getElementById('str1').value;
let letter = document.getElementById('letter1').value;
if (!str || !letter) {
document.querySelector('p').innerText = "Please enter both string and character";
return;
}
// Use the first character only
let char = letter[0];
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
document.querySelector('p').innerText = "'" + char + "' appears " + count + " times";
}
</script>
Enter String: <input type="text" id="str1" placeholder="Enter your text"><br><br>
Enter Character: <input type="text" id="letter1" placeholder="Enter character"><br><br>
<button onclick="displayCount()">Count Occurrences</button><br><br>
Result: <p></p>
</body>
</html>
Comparison of Methods
| Method | Performance | Readability | Best For |
|---|---|---|---|
| For Loop | Fast | High | Simple counting |
| split() | Medium | High | Quick one-liner |
| Regex | Medium | Medium | Complex patterns |
Conclusion
The for loop method is the most straightforward and efficient for counting single characters. Use split() for simplicity or regex for complex pattern matching. All methods work effectively for basic character counting tasks.
Advertisements
