Nodejs String Trim trimSpaces()

Here you can find the source of trimSpaces()

Method Source Code

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];// ww w.ja  va  2 s  . c om
         } else {
            result += " " + phrases[i];
         }
      }
   }
   return result;
}

Related

  1. 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(); 
    ...
    
  2. trimBoth(s)
    String.prototype.trimBoth = function(s) {
        return this.replace(new RegExp("^" + s + "+|" + s + "+$", "gm"), "");
    
  3. trimChars(charlist)
    String.prototype.trimChars = function(charlist) {
      charlist = charlist || ' \r\n\t';
        var l = 0, i = 0;
        var ret = this;
        l = ret.length;
        for (i = 0; i < l; i++) {
            if (charlist.indexOf(ret.charAt(i)) === -1) {
                ret = ret.substring(i);
                break;
    ...
    
  4. 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;
    ...
    
  5. 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) : "";
    };
    ...
    
  6. 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);
    
  7. trimStart(s)
    String.prototype.trimStart = function(s) {
        return this.replace(new RegExp("^" + s + "+", "gm"), "");
    
  8. trimString_trim()
    String.prototype.trim = function String_trim()
      this.replace(/^\s+/m, '').replace(/\s+$/m, '');
    
  9. trimTailChars(c)
    String.prototype.trimTailChars = function(c){
      var idx, len = this.length;
      var tmp = this;
      while(len--){
        idx = tmp.lastIndexOf(c);
        if(idx == -1) {
          break;
        tmp = tmp.substring(0,idx);
    ...