Nodejs String Trim trimChars(charlist)

Here you can find the source of trimChars(charlist)

Method Source Code

String.prototype.trimChars = function(charlist) {
   charlist = charlist || ' \r\n\t';
    var l = 0, i = 0;
    /*from w  ww  .j  av a  2 s.c  o m*/
    var ret = this;
    
    l = ret.length;
    for (i = 0; i < l; i++) {
        if (charlist.indexOf(ret.charAt(i)) === -1) {
            ret = ret.substring(i);
            break;
        }
    }
    
    l = ret.length;
    for (i = l - 1; i >= 0; i--) {
        if (charlist.indexOf(ret.charAt(i)) === -1) {
            ret = ret.substring(0, i + 1);
            break;
        }
    }
    
    return charlist.indexOf(ret.charAt(0)) === -1 ? ret : '';
};

Related

  1. trim(str)
    String.prototype.trim = function(str) {
      str = str.replace(/^\s+/,'');
      for( var i = str.length - 1 ; i >= 0 ; i--) {
        if(/\S/.test(str.charAt(i))) {
            str = str.substring(0,i+1);
            break;
      return str;
    ...
    
  2. trim(str)
    var trim = function (str) {
        return str.replace(/\s*/g, "");
    };
    
  3. trim(ws)
    String.prototype.trim = function (ws)
        if(!this.length) return "";
        var tmp = this.stripNL().ltrim().rtrim();
        if(ws) return tmp.replace(/ +/g, ' ');
        else return tmp;
    String.prototype.rtrim = function ()
        if(!this.length) return "";
        return this.replace(/\s+$/g, '');
    String.prototype.ltrim = function ()
        if(!this.length) return "";
        return this.replace(/^\s+/g, '');
    String.prototype.stripNL = function ()
        if(!this.length) return "";
        return this.replace(/[\n\r]/g, '');
    
  4. trim2()
    String.prototype.trim2 = function() {
      return this.replace(/^\s+|\s+$/g, '');
    };
    String.prototype.trim = function() {
      return this.replace(/^\s+/, '').replace(/\s+$/, '');
    };
    'foo     '.trim(); 
    '     foo'.trim(); 
    '  foo   '.trim(); 
    ...
    
  5. trimBoth(s)
    String.prototype.trimBoth = function(s) {
        return this.replace(new RegExp("^" + s + "+|" + s + "+$", "gm"), "");
    
  6. trimSlash()
    String.prototype.trimSlash = function () {
        var string = this.trim();
        if (string.charAt(0) == "/") {
            string = string.substring(1);
        if (string.charAt(string.length - 1) == "/") {
            string = string.substring(0, string.length - 1);
        return string;
    ...
    
  7. trimSpace()
    'use strict';
    String.prototype.trimSpace = function() {
        var s = this;
        var n = s.length - 1;
        while (n >= 0 && s.charCodeAt(n) == 32) {
            n--;
        return n >= 0 ? s.substr(0, n + 1) : "";
    };
    ...
    
  8. trimSpaces()
    String.prototype.trimSpaces = function () {
      var phrases = this.split(" ");
      var result = "";
      for (var i = 0; i < phrases.length; i++) {
        if (!phrases[i]=="") {
          if (result=="") {
            result = phrases[i];
          } else {
            result += " " + phrases[i];
    ...
    
  9. trimStart(c)
    String.prototype.trimStart=function(c)
        c = c?c:' ';
        var i=0;
        for(;i<this.length && this.charAt(i)==c; i++);
        return this.substring(i);