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 :: push to an array javascript 
Javascript :: type js 
Javascript :: event.target 
Javascript :: Update an object as state with React hooks 
Javascript :: vanilla js 
Javascript :: Lazy Loading 
Javascript :: why we import react from react 
Javascript :: setting up react on visual studio code 
Javascript :: monngoose find from an array using in 
Javascript :: gitea 
Javascript :: using for loops js 
Javascript :: jsoup 
Javascript :: react native generate signed apk getting older version 
Javascript :: javascript css 
Javascript :: base 8 number javascript 
Javascript :: data table buttons 
Javascript :: destructuring assignment in javascript 
Javascript :: array js 
Javascript :: make triangle with threejs 
Javascript :: add role command discord.js 
Javascript :: call node.js file electron 
Javascript :: js date toisostring with timezone 
Javascript :: quadratic equation in js by function 
Javascript :: even numbers in an array 
Javascript :: linkedin api v2 get email address 
Javascript :: js create jaon object from for loop 
Javascript :: js access sql database on server 
Javascript :: is already declared javascript 
Javascript :: bigint type js 
Python :: discord bot status python 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =