Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript count occurrences of letter in string

function count(str, find) {
    return (str.split(find)).length - 1;
}

count("Good", "o"); // 2
Comment

count occurrences of character in string javascript

var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
console.log(count);

Output: 2

Explaination : 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.
Comment

javascript count occurence of character in string

// String Prototype: myCount = myString.polycount("countThis")
String.prototype.polycount = function (criteria) {
  return this.split(criteria).length - 1 }
// Arrow Function (STR): myCount = strCount(myString, "countThis")
const strCount = (stack, find) => stack.split(find).length - 1
// Array Prototype: myCount = myArray.polycount("countThis")
Array.prototype.polycount = function (criteria) {
  return this.join("").split(criteria).length - 1 }
// Arrow Function (ARR): myCount = arrCount(myArray, "countThis")
const arrCount = (stack, find) => stack.join("").split(find).length - 1
Comment

Javascript count instances of character in a string

 function countInstancesOf(letter, sentence) {
  var count = 0;

  for (var i = 0; i < sentence.length; i++) {
    if (sentence.charAt(i) === letter) {
      count += 1;
    }
  }
  return count;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: call dynamic var name javascript 
Javascript :: push notification react native 
Javascript :: leafletjs openstreets example 
Javascript :: modal example react native 
Javascript :: how to extract strings in array js 
Javascript :: check whether array ascending 
Javascript :: IntersectionObserver polyfill 
Javascript :: data-parsley-errors-container 
Javascript :: how to make html with jquery 
Javascript :: Find the Longest Word in a String 
Javascript :: jquery steps disable finish button 
Javascript :: cypress check element has an attribute 
Javascript :: js number to string 
Javascript :: leafletjs code 
Javascript :: how to get ip address and port from url in javascript 
Javascript :: javascript reduce return array 
Javascript :: find the max number in an array js 
Javascript :: change a css class in javascript 
Javascript :: if text is present make div hide 
Javascript :: react-native-image-picker npm 
Javascript :: react firebase add doc to collection 
Javascript :: nodejs remove element from array 
Javascript :: what is prototype javascript 
Javascript :: of() angular 
Javascript :: batch react-redux 
Javascript :: Laravel react 404 routes 
Javascript :: how to combine two regular expressions in javascript 
Javascript :: js date format 
Javascript :: create new project angular 
Javascript :: javaScript getMonth() Method 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =