Quantcast
Channel: How to count string occurrence in string? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by Mr. Doge for How to count string occurrence in string?

$
0
0

added this optimization:

How to count string occurrence in string?

This is probably the fastest implementation here, but it would be even faster if you replaced "++pos" with "pos+=searchFor.length" –hanshenrik

function occurrences(str_, subStr) {  let occurence_count = 0  let pos = -subStr.length  while ((pos = str_.indexOf(subStr, pos + subStr.length)) > -1) {    occurence_count++  }  return occurence_count}

Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>