Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts

    How to properly use trim

    This is what i have, is this correct?

    if the client enters spaces after the input i want to trim it

    Code:
    //in this example the input is blue_ _ _   (_=spaces)
    
    var inp = document.subregfrm.squest.value; 
    var answ = inp.trim();
    
    //answ should = blue   (no spaces)
    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    The trim() function is native to PHP (and other languages) but the Javascript inbuilt function String.trim() is implemented only in the most modern browsers. Prefer:-

    Code:
    var inp = "   abc   "
    var answ = inp.replace(/^\s+|\s+$/g,"");  // the g switch is essential!!
    or if you prefer something re-usable:-

    Code:
    function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
    }
    Quizmaster: Since 1920 a statue of which former US President has resided in Parliament Square?
    Contestant: Ronald Reagan
    Last edited by Philip M; 01-21-2013 at 01:45 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    durangod (01-21-2013)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    for performance, readability, and consistency, one should always use the native syntax.

    Its actually been several years now that String.prototoype.trim() has been bundled with browsers.

    if you need backwards-compatibility (aka graceful degradation) use a ployfill to make sure the String method is the same as it is in all current versions of all major browsers:

    in the case of trim(), we can do it 100% backwards compatible, so there's no reason to have to balance an extra pair of parens when the proto is so handy:

    Code:
    "".trim|| (String.prototype.trim = function trim() {
      return this.replace(/^\s+|\s+$/g,"");
    });

    if you already hard-coded trim(str) into your code, you can speed up your flow by re-binding the native:


    Code:
    var trim=eval.call.bind("".trim);
    
    //example usage:
    trim( "  hello   ");
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    String.prototoype.trim() is not implemented in IE<9. I see nothing wrong with the regex I suggested.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by Philip M View Post
    String.prototoype.trim() is not implemented in IE<9. I see nothing wrong with the regex I suggested.
    right, which is why i used your regex and posted a polyfill version allowing you to use String.prototype.trim() in IE 5-8...

    if you want to bench the two, you'll find the native version, which runs in C, executes dozens of times faster than a user regexp.

    but above performance, its about coding style. why write custom code using a proprietary syntax when you can easily use the native syntax, even in 3-version old IE using the code i posted.

    protos are easier to code because they are an after-thought, added to the right of a string expression, whereas functions have to wrap the expression. its much quicker to type [SHIFT]+[9][0] than two separate shift presses and some arrow keys, and protos separate content from presentation with a dot.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •