Nodejs String Sub String substrCount(needle)

Here you can find the source of substrCount(needle)

Method Source Code

/**//from  www.ja  va 2s  . com
@Name: String.prototype.substrCount
@Author: Paul Visco
@Version: 1.1 11/19/07 09/18/08
@Description: Returns the numbers of times a substring is found in a string
@Param: String needle The substring to search for within the string
@Return: Number The number of times the substring is found in the original string
@Example:
var myString = 'hello world on earth';

var answer = myString.substrCount('world');
//answer = 1;
*/
String.prototype.substrCount = function(needle){
   var cnt = 0;
   for (var i=0;i<this.length;i++) {
   if (needle == this.substr(i,needle.length))
      cnt++;
   } 
   return cnt;
};

Related

  1. subst(replacements)
    String.prototype.subst = function(replacements) {
        return this.replace(/%[A-Z][A-Z0-9_]+%/g, function (placeholder) {
            return placeholder in replacements ? replacements[placeholder] : placeholder;
        });
    };
    
  2. subst(replacements)
    String.prototype.subst = function (replacements) {
        "use strict";
        return this.replace(/%[A-Z][A-Z0-9_]+%/g, function (placeholder) {
            return placeholder in replacements ? replacements[placeholder] : placeholder;
        });
    };
    
  3. substitute(object, regexp)
    String.prototype.substitute = function(object, regexp){
      return String(this).replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
        if (match.charAt(0) == '\\') return match.slice(1);
        return (object[name] !== null) ? object[name] : '';
      });
    };
    
  4. substr(begin, end)
    String.prototype.substr = function(begin, end) {
      if (end === null) { end = this.length;           }
      if (begin <  0)   { begin = this.length + begin; }
      if (end <= 0)     { end = this.length + end;     }
      if (begin < 1)    { begin = end - 1/begin;       }
      if (end < 1)      { end = begin + 1/end;         }
      return this.substring(begin, end);
    
  5. substr(start, length)
    String.prototype.substr = function(start, length) {
      if (start < 0) {
        start = start + this.length;
      if (!length) {
        length = this.length - start;
      var s = "";
      for (var i = start, len = this.length; i < start + length && i < len; i+) {
    ...
    
  6. substrReplace(replaceWith, start, length)
    String.prototype.substrReplace = function(replaceWith, start, length){
      return this.replace(this.substring(start, (start+length)), replaceWith );
    };
    
  7. substrWoR(c)
    String.prototype.substrWoR = function(c){
      var n=1,o=0;
      while(o!=n){
        s=this.substr(0,c+(o=n)-1);
        n=s.split("\r").length;
      return s;
    
  8. substrb(len)
    String.prototype.substrb = function(len){
      var str1;
      if(this.lengthb() <= len)
        return this;
      for(var i = len/2; i < this.length; i++)
        str1 = this.substr(0,i);
    ...
    
  9. substring(start, end)
    String.prototype.substring = function(start, end) {
      if (start < 0) {
        start = start + this.length;
      if (!end) {
        end = this.length;
      } else if (end < 0) {
        end = end + this.length;
      var s = "";
      for (var i = start, len = this.length; i < end && i < len; i++) {
        s += this[i];
      return s;
    };