From 4dff9197c64f62188b8059b6b86fc2c4b102a3e4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 31 May 2017 10:54:19 +0200 Subject: [PATCH] Added substr_count --- Utils/oLib.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Utils/oLib.js b/Utils/oLib.js index aae9a43..2a47dbf 100644 --- a/Utils/oLib.js +++ b/Utils/oLib.js @@ -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