Added substr_count

This commit is contained in:
Dennis Eichhorn 2017-05-31 10:54:19 +02:00 committed by GitHub
parent 49589a115d
commit 4dff9197c6

View File

@ -23,6 +23,32 @@
{
return str.replace(new RegExp("^[" + char + "]*"), '');
};
jsOMS.substr_count = function(str, substr) {
str += '';
substr += '';
if (substr.length <= 0) {
return (str.length + 1);
}
let n = 0,
pos = 0,
step = substr.length;
while (true) {
pos = str.indexOf(substr, pos);
if (pos >= 0) {
++n;
pos += step;
} else {
break;
}
}
return n;
}
/**
* Class finder