Trim string in JavaScript?

后端 未结 20 2343
不知归路
不知归路 2020-11-21 06:27

How do I trim a string in JavaScript? That is, how do I remove all whitespace from the beginning and the end of the string in JavaScript?

相关标签:
20条回答
  • 2020-11-21 06:52

    You can do it using the plain JavaScript:

    function trimString(str, maxLen) {
    if (str.length <= maxLen) {
    return str;
    }
    var trimmed = str.substr(0, maxLen);
    return trimmed.substr(0, trimmed.lastIndexOf(' ')) + '…';
    }
    
    // Let's test it
    
    sentenceOne = "too short";
    sentencetwo = "more than the max length";
    
    console.log(trimString(sentenceOne, 15));
    console.log(trimString(sentencetwo, 15));
    
    0 讨论(0)
  • 2020-11-21 06:53

    All browsers since IE9+ have trim() method for strings:

    " \n test \n ".trim(); // returns "test" here
    

    For those browsers who does not support trim(), you can use this polyfill from MDN:

    if (!String.prototype.trim) {
        (function() {
            // Make sure we trim BOM and NBSP
            var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
            String.prototype.trim = function() {
                return this.replace(rtrim, '');
            };
        })();
    }
    

    That said, if using jQuery, $.trim(str) is also available and handles undefined/null.


    See this:

    String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
    
    String.prototype.ltrim=function(){return this.replace(/^\s+/,'');};
    
    String.prototype.rtrim=function(){return this.replace(/\s+$/,'');};
    
    String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};
    
    0 讨论(0)
  • 2020-11-21 06:58

    If you are using jQuery, use the jQuery.trim() function. For example:

    if( jQuery.trim(StringVariable) == '')
    
    0 讨论(0)
  • 2020-11-21 06:58

    Trim code from angular js project

    var trim = (function() {
    
      // if a reference is a `String`.
      function isString(value){
           return typeof value == 'string';
      } 
    
      // native trim is way faster: http://jsperf.com/angular-trim-test
      // but IE doesn't have it... :-(
      // TODO: we should move this into IE/ES5 polyfill
    
      if (!String.prototype.trim) {
        return function(value) {
          return isString(value) ? 
             value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
        };
      }
    
      return function(value) {
        return isString(value) ? value.trim() : value;
      };
    
    })();
    

    and call it as trim(" hello ")

    0 讨论(0)
  • 2020-11-21 06:58

    I have a lib that uses trim. so solved it by using the following code.

    String.prototype.trim = String.prototype.trim || function(){ return jQuery.trim(this); };
    
    0 讨论(0)
  • 2020-11-21 06:58

    For IE9+ and other browsers

    function trim(text) {
        return (text == null) ? '' : ''.trim.call(text);
    }
    
    0 讨论(0)
提交回复
热议问题