Skip to content

Instantly share code, notes, and snippets.

@tiberiur
Last active April 13, 2016 11:58
Show Gist options
  • Save tiberiur/9630e310aaf439296900 to your computer and use it in GitHub Desktop.
Save tiberiur/9630e310aaf439296900 to your computer and use it in GitHub Desktop.
[JS] Counting Occurrences of Substrings
function countingOccurrences(string, subString)
{
var foundAtPosition = 0;
var count = 0;
while (foundAtPosition != -1) {
foundAtPosition = string.indexOf(subString, foundAtPosition);
if (foundAtPosition != -1) {
count++;
foundAtPosition++;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment