Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Function count the occurrences of substring in a string;
- * @param {String} string Required. The string;
- * @param {String} subString Required. The string to search for;
- * @param {Boolean} allowOverlapping Optional. Default: false;
- */
- function occurrences(string, subString, allowOverlapping) {
- string += "";
- subString += "";
- if (subString.length <= 0) return string.length + 1;
- var n = 0, pos = 0;
- var step = (allowOverlapping) ? (1):(subString.length);
- while (true) {
- pos = string.indexOf(subString, pos);
- if (pos >= 0) {
- n++;
- pos += step;
- } else break;
- }
- return(n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement