Nodejs String Sub String subStringCounter(searchString)

Here you can find the source of subStringCounter(searchString)

Method Source Code

//Write a JavaScript function that finds how many times a substring is contained in a given text (perform case insensitive search).

var text = 'this is  test test test we in are is';

String.prototype.subStringCounter = function (searchString) {
    var position = 0,
        counter = 0;// w ww.  j  a va 2  s.  c o  m

    searchString = searchString.toLowerCase();

    while (this.indexOf(searchString, position) >= 0) {
        counter += 1;
        position = this.toLowerCase().indexOf(searchString, position) + 1;
    }

    return counter;
};

console.log(text.subStringCounter('in'));
console.log(text.subStringCounter('we'));

Related

  1. sub(pattern, replacement, count)
    String.prototype.sub = function(pattern, replacement, count) {
      replacement = this.gsub.prepareReplacement(replacement);
      count = count === undefined ? 1 : count;
      return this.gsub(pattern, function(match) {
        if (--count < 0) return match[0];
        return replacement(match);
      });
    };
    
  2. sub(pattern, replacement, count)
    String.prototype.sub = function(pattern, replacement, count) {
      var replacer = Object.isFunction(replacement) ?
        replacement :
        function(){ return replacement };
      count = Object.isUndefined(count) ? 1 : count;
      return this.gsub(pattern, function(match) {
        if (--count < 0) return match[0];
        return replacer(match);
      });
    ...
    
  3. subString()
    String.prototype.subString = function(){
        var arr = [];
        for(var i = 0; i < this.length; i++){
            for(var j = i + 1; j < this.length + 1; j++){
                arr.push(this.slice(i, j));
        return arr;
    console.log("dog".subString());
    
  4. subString()
    String.prototype.subString = function()   
      var subset = [];  
      for (var m = 0; m < this.length; m++)   
        for (var n = m+1; n<this.length+1; n++)   
          subset.push(this.slice(m,n));  
      return subset;  
    };  
    console.log("dog".subString());
    
  5. subStrings()
    String.prototype.subStrings = function () {
      var substrings = [];
      for (var i = 0; i < this.length ; i++) {
        for (var j = i+1; j <= this.length; j++) {
          substrings.push(this.slice(i,j));
        };
      };
      return substrings;
    };
    ...
    
  6. subStrings()
    String.prototype.subStrings = function () {
      var subs = [];
      for (var i = 0; i < this.length; i++) {
        for (var j = i + 1; j < this.length; j++) {
          subs.push(this.slice(i, (j + 1)));
      return subs;
    };
    ...
    
  7. subStrings()
    String.prototype.subStrings = function () {
      var subs = [];
      for (var i = 0; i < this.length; i++) {
        for (var j = i+1; j <= this.length; j++) {
          subs.push(this.slice(i,j));
        };
      };
      var sorted = subs.sort();
      var uniques = [sorted[0]];
    ...
    
  8. subStrings()
    String.prototype.subStrings = function () {
      result = [];
      for (var beginning = 0; beginning < this.length; beginning++) {
        for (var end = beginning + 1; end < this.length + 1; end++) {
          result.push(this.slice(beginning, end));
      return result
    var a = "cat";
    var b = a.subStrings();
    console.log(b);