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 :: store array in local storage js 
Javascript :: empty text in all class jquery 
Javascript :: hasOwnProperty with more than one property javascript 
Javascript :: get number from string javascript 
Javascript :: javascript date get current date 
Javascript :: get the size of the screen javascript 
Javascript :: ng serve without reload 
Javascript :: .map for object javscript 
Javascript :: javascript auto save input 
Javascript :: check balance of a wallet in js 
Javascript :: double matrix iteration in react 
Javascript :: javascript append how first element 
Javascript :: eslint react native 
Javascript :: for loop array javascript 
Javascript :: js iterate dict 
Javascript :: body-parser deprecated bodyParser 
Javascript :: ngfor select angular 
Javascript :: inline style boarder radius jsx 
Javascript :: easyui datagrid double click cell 
Javascript :: jquery add disabled to id 
Javascript :: input pattern for no whitespaces at the end or beginning 
Javascript :: jquery modal on show + target button 
Javascript :: find object length in javascript 
Javascript :: loop through each class jq 
Javascript :: how to get orientation in js 
Javascript :: convert hex to decimal javascript 
Javascript :: js do after delay 
Javascript :: can immigrants vote in uk 
Javascript :: jQuery select immediate children 
Javascript :: brackets not autocompleting in js file in vscode 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =