“Zliczenie wystąpień znaków w String JavaScript” Kod odpowiedzi

Liczba JavaScript Występowanie litery w ciągu

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

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

Zliczenie wystąpień znaków w 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.
Ankur

Liczba JavaScript Wystąpienia w ciągu

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

Wartość liczby A do B JavaScript

function count (string) {  
  var count = {};
  string.split('').forEach(function(s) {
     count[s] ? count[s]++ : count[s] = 1;
  });
  return count;
}
Restu Wahyu Saputra

Wartość liczby A do B JavaScript

 const recorrences = ['a', 'b', 'c', 'a', 'b','a']
                .map(i => !!~i.indexOf('a'))
                .filter(i => i)
                .length;
console.log(`recorrences ${recorrences}`) 
//recorrences 3
Restu Wahyu Saputra

Występowanie liczby JavaScript znaku w ciągu

// 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

Odpowiedzi podobne do “Zliczenie wystąpień znaków w String JavaScript”

Pytania podobne do “Zliczenie wystąpień znaków w String JavaScript”

Więcej pokrewnych odpowiedzi na “Zliczenie wystąpień znaków w String JavaScript” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu