Nodejs String Trim trimTailChars(c)

Here you can find the source of trimTailChars(c)

Method Source Code

// this is slow, avoid if possible
String.prototype.trimTailChars = function(c){
  var idx, len = this.length;
  var tmp = this;
  while(len--){// w  w w.jav  a  2 s  . c om
    idx = tmp.lastIndexOf(c);
    if(idx == -1) {
      break;
    }
    tmp = tmp.substring(0,idx);
  }
  return tmp;
};

String.prototype.trimTailChar = function(c){
  var idx, len = this.length;
  var tmp = this;
  while(len--){
     idx = tmp.charAt(len);
     if(idx != c){
       break;
     }
  }
  return tmp.substring(0,len + 1);
};

String.prototype.countTailChar = function(c){
  var idx, len = this.length;
  while(len--){
     idx = this.charAt(len);
     if(idx != c){
       break;
     }
  }
  return this.length-(len + 1);
}

// too complicated, not worth the hassle
//String.prototype.trimLeadChars = function(c){};

String.prototype.trimLeadChar = function(c){
  var idx, len = 0;
  var tmp = this;
  while(len++ < this.length){
     idx = tmp.charAt(len);
     if(idx != c){
       break;
     }
  }
  return tmp.substring(len,this.length-1);
};

Related

  1. 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) : "";
    };
    ...
    
  2. 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];
    ...
    
  3. 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);
    
  4. trimStart(s)
    String.prototype.trimStart = function(s) {
        return this.replace(new RegExp("^" + s + "+", "gm"), "");
    
  5. trimString_trim()
    String.prototype.trim = function String_trim()
      this.replace(/^\s+/m, '').replace(/\s+$/m, '');
    
  6. trimToLength(m)
    String.prototype.trimToLength = function(m) {
      return (this.length > m) 
        ? jQuery.trim(this).substring(0, m).split(" ").slice(0, -1).join(" ") + "..."
        : this;
    };
    
  7. trimToLength(m)
    String.prototype.trimToLength = function(m) {
      return (this.length > m) 
        ? jQuery.trim(this).substring(0, m).split(" ").slice(0, -1).join(" ") + "..."
        : this;
    };
    
  8. trimToLength(m)
    String.prototype.trimToLength = function(m) {
      return (this.length > m) ? jQuery.trim(this).substring(0, m).split(' ').slice(0, -1).join(' ') : this.replace('"', '');
    };
    
  9. trimToPx(length)
    String.prototype.trimToPx = function(length)
        var tmp = this;
        var trimmed = this;
        if (tmp.visualLength() > length)
            trimmed += "...";
            while (trimmed.visualLength() > length)
                tmp = tmp.substring(0, tmp.length-1);
                trimmed = tmp + "...";
        return trimmed;