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

Answer by Jason Larke for How to count string occurrence in string?

$
0
0

Super duper old, but I needed to do something like this today and only thought to check SO afterwards. Works pretty fast for me.

String.prototype.count = function(substr,start,overlap) {    overlap = overlap || false;    start = start || 0;    var count = 0,         offset = overlap ? 1 : substr.length;    while((start = this.indexOf(substr, start) + offset) !== (offset - 1))++count;    return 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>