Count substrings occurrences

    0

    1

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Counts the occurrences of a substring in a given string.

    const countSubstrings = (str: string, searchValue: string) => {
      let count: number = 0,
        i: number = 0;
      while (true) {
        const r: number = str.indexOf(searchValue, i);
        if (r !== -1) [count, i] = [count + 1, r + 1];
        else return count;
      }
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.