
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to count the number of occurrences of a character in a string in JavaScript?
To count the number of occurrences of a character in a string, try to run the following code −
Example
<!DOCTYPE html> <html> <body> <script> function displayCount() { setTimeout(function() { var str = document.getElementById('str1').value; var letter = document.getElementById('letter1').value; letter = letter[letter.length-1]; var lCount; for (var i = lCount = 0; i < str.length; lCount += (str[i++] == letter)); document.querySelector('p').innerText = lCount; return lCount; }, 50); } </script> Enter String: <input type="text" id="str1"><br><br> Enter Letter: <input onkeypress="displayCount()" type="text" id="letter1"><br><br> <button onclick="displayCount()">Click for Result</button><br><br> result= <p></p> </body> </html>
- Related Questions & Answers
- Count occurrences of a character in string in Python
- Count occurrences of a character in a repeated string in C++
- How to Count Occurrences of Each Character in String in Android?
- Count number of occurrences for each char in a string with JavaScript?
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- C# program to count the occurrences of each character
- Java program to count the occurrences of each character
- Python program to count occurrences of a word in a string
- C# program to count occurrences of a word in string
- Java program to count occurrences of a word in string
- How to replace all occurrences of a string in JavaScript?
- How to count number of occurrences of repeated names in an array - JavaScript?
- Write a python program to count occurrences of a word in string?
- Java Program to replace all occurrences of a given character in a string
- Finding number of occurrences of a specific string in MySQL?
Advertisements