“JavaScript Count Octincement of Caractor in String” Réponses codées

JavaScript Count Occurrences of Letter in String

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

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

compter les occurrences de caractère en javascript de chaîne

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.
Ankur

JavaScript Count Occurrences in String

function countOccurences(string, word) {
   return string.split(word).length - 1;
}
var text="We went down to the stall, then down to the river."; 
var count=countOccurences(text,"down"); // 2
Grepper

JavaScript Count Octincement of Caractor 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
Brian Patterson

JavaScript Count Instances de caractère dans une chaîne

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

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

Réponses similaires à “JavaScript Count Octincement of Caractor in String”

Questions similaires à “JavaScript Count Octincement of Caractor in String”

Plus de réponses similaires à “JavaScript Count Octincement of Caractor in String” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code