“ocurrencias de conteo de JavaScript en la cadena” Código de respuesta

JavaScript Counting Octents of Letter en String

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

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

Cuente las ocurrencias de carácter en 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

ocurrencias de conteo de JavaScript en la cadena

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 Ocurrencia de carácter en cadena

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

JS String incluye el recuento

/** Function that count occurrences of a substring in a string;
 * @param {String} string               The string
 * @param {String} subString            The sub string to search for
 * @param {Boolean} [allowOverlapping]  Optional. (Default:false)
 *
 * @author Vitim.us https://gist.github.com/victornpb/7736865
 * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
 * @see http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
 */
function occurrences(string, subString, allowOverlapping) {

    string += "";
    subString += "";
    if (subString.length <= 0) return (string.length + 1);

    var n = 0,
        pos = 0,
        step = allowOverlapping ? 1 : subString.length;

    while (true) {
        pos = string.indexOf(subString, pos);
        if (pos >= 0) {
            ++n;
            pos += step;
        } else break;
    }
    return n;
}
Glamorous Gharial

Ocurrencias de conteo de JavaScript en la cadena

// No Regex Version (With Overlapping)
// str => input string
// sub => input substring
// returns number of occurrences of sub in str with overlaping

const countOccurrences = (str, sub) => {
    let count = 0;
    pos = str.indexOf(sub);

    while(pos>=0){
        count++;
        pos++;
        pos = str.indexOf(sub, pos)
    }
    return count;
}
Joao Verçosa

Respuestas similares a “ocurrencias de conteo de JavaScript en la cadena”

Preguntas similares a “ocurrencias de conteo de JavaScript en la cadena”

Más respuestas relacionadas con “ocurrencias de conteo de JavaScript en la cadena” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código