Nodejs String Remove removeSubstring(a, b)

Here you can find the source of removeSubstring(a, b)

Method Source Code

String.prototype.removeSubstring = function(a, b){
   var array = this.split("");
   var w = 0;//  w  w w  . j a  v a2 s .  c om
   var str = [];
   if (b !== undefined){
      array.forEach(function (elm) {
            if (elm === a & w < b){
              w += 1;
            } else {
               str.push(elm);
            }
       });
   } else {
      array.forEach(function (elm) {
            if (elm !== a){
               str.push(elm);
            }
       });
   }

   return str.toString();;

}

//------------------- Testing -----------------------------------//
var str1 = 'aaaaa';
var newStr1 = str1.removeSubstring('a', 3); // newStr = 'a' 
console.log( newStr1 );
var str2 = 'aaabbbbccc'; 
var newStr2 = str2.removeSubstring('b'); // newStr = 'bbbb' 
console.log( newStr2 );
//------------------- Testing -----------------------------------//

Related

  1. removeSpace()
    String.prototype.removeSpace=function(){
      return this.replace(/\s/g, "");
    
  2. removeSpaces()
    String.prototype.removeSpaces = function() {
      return this.replace(/\s+/g, '');
    };
    String.prototype.hashCode = function(){
      var hash = 0;
        if (this.length == 0) return hash;
        for (var i = 0; i < this.length; i++) {
            char = this.charCodeAt(i);
            hash = ((hash<<5)-hash)+char;
    ...
    
  3. removeSpaces()
    String.prototype.removeSpaces = function() {
      return this.replace(/\s+/g, '');
    };
    
  4. removeSpacesAndToLower()
    String.prototype.removeSpacesAndToLower = function () {
      return this.replace(/\s+/g, '-').toLowerCase();
    
  5. removeSpecialCharacter()
    String.prototype.removeSpecialCharacter = function(){
      return this.replace(/[^a-zA-Z0-9_]/g, '');
    
  6. removeSubstring(a, n)
    String.prototype.removeSubstring = function (a, n) {
      var arr = [];
      arr = str.split('');
      arr.sort();
      console.log("oldStr = '" + str + "'");
      var count = 0;
      for (var i = 0; i < arr.length; i++) {
        if( n != null ) {
          if(arr[i] === a)
    ...
    
  7. removeSubstring(arg1, b)
    String.prototype.removeSubstring = function(arg1, b) {
      if (b) {
        var temp = arg1;
        for(var i=1;i<b;i++) {
          arg1 += temp;
      return this.replace(new RegExp(arg1, 'g'), '');
    var str1 = 'aaa';
    var newStr1 = str1.removeSubstring('a', 2);
    console.log(newStr1);
    var str2 = 'aaabbb';
    var newStr2 = str2.removeSubstring('a');
    console.log(newStr2);
    
  8. removeSubstring(char,occurance)
    String.prototype.removeSubstring = function(char,occurance) 
      var temp='';
      if(occurance==undefined)
        for (var i = 0; i < this.length; i++) 
          if(char!==this[i])
            temp+=this[i];
      else
        var flag = true;
        var ct=parseInt("0");
        for (var i = 0; i < this.length ; i++) 
          if(char===this[i])
            ct++;
            flag=(ct===parseInt(occurance))?false:true;
          else
            temp+=this[i];
          if(!flag)
            temp+=this.substr(i+1,this.length);
            return temp;
      return temp;
    };
    var str1 = 'aaa';
    console.log(str1.removeSubstring('a', 2)); 
    var str2 = 'aaabbbb'; 
    console.log(str2.removeSubstring('a'));
    
  9. removeSubstring(start, length)
    String.prototype.removeSubstring = function(start, length) {
        return this.substr(0, start) + this.substr(start + length);
    };