Subtract the characters of a given string from another string - Node.js String

Node.js examples for String:Char

Description

Subtract the characters of a given string from another string

Demo Code


/* Subtract the characters of a given string from another string
 * /*from   www . j a  v a2  s .com*/
 * "Abracadabra".subtract('arbd')  // => "Acaabra"
 */
String.prototype.subtract = function ( chars ) {
  var out = '', 
      str = chars + '';
  if ( arguments.length > 1 ) {
    str = Array.prototype.join.call( arguments, '' );
  }
  for (var i=0; i<this.length; i++) {
    var ch = this.charAt( i );
    if ( str.indexOf( ch ) !== -1 ) {
      str = str.replace( ch, '' );
    }
    else {
      out += ch;
    }
  }
  return out;
};

Related Tutorials