FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?

Discussion in 'Javascript' started by FAQ server, Aug 24, 2007.

  1. FAQ server

    FAQ server Guest

    -----------------------------------------------------------------------
    FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
    -----------------------------------------------------------------------

    Using Regular Expressions (JavaScript 1.2/JScript 4+) :

    String.prototype.lTrim =
    function()
    {
    return this.replace(/^\s+/,'');
    }
    String.prototype.rTrim =
    function()
    {
    return this.replace(/\s+$/,'');
    }
    String.prototype.trim =
    function()
    {
    return this.replace(/^\s+|\s+$/g,'');
    }

    or for all versions (trims characters ASCII<32 not true
    "whitespace"):

    function LTrim(str) {
    for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
    return str.substring(k,str.length);
    }
    function RTrim(str) {
    for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
    return str.substring(0,j+1);
    }
    function Trim(str) {
    return LTrim(RTrim(str));
    }

    http://msdn.microsoft.com/library/d...html/2380d458-3366-402b-996c-9363906a7353.asp

    http://docs.sun.com/source/816-6408-10/regexp.htm


    ===
    Postings such as this are automatically sent once a day. Their
    goal is to answer repeated questions, and to offer the content to
    the community for continuous evaluation/improvement. The complete
    comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
    The FAQ workers are a group of volunteers. The sendings of these
    daily posts are proficiently hosted by www.pair.com.
     
    FAQ server, Aug 24, 2007
    #1
    1. Advertising

  2. FAQ server wrote:
    > -----------------------------------------------------------------------
    > FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
    > -----------------------------------------------------------------------
    >
    > Using Regular Expressions (JavaScript 1.2/JScript 4+) :
    >
    > String.prototype.lTrim =
    > function()
    > {
    > return this.replace(/^\s+/,'');
    > }
    > String.prototype.rTrim =
    > function()
    > {
    > return this.replace(/\s+$/,'');
    > }
    > String.prototype.trim =
    > function()
    > {
    > return this.replace(/^\s+|\s+$/g,'');
    > }


    I have tested the following requirements to be available as of JScript
    _3.1.3510_ (in standalone IE 4.01/WinNT [1]):

    - Anonymous function expression
    - String.prototype.replace()
    - RegExp literal with `g' modifier, and without `i' modifier
    - Replacing substrings matching against the aforementioned RegExp object

    I have retrieved the JScript version information through ScriptEngine(),
    ScriptEngineMajorVersion(), ScriptEngineMinorVersion(), and
    ScriptEngineBuildVersion(), and tested several newer features (like
    try...catch) to be *not* supported by that script engine. Therefore,
    I am confident I got valid results.

    I have updated http://PointedEars.de/scripts/es-matrix accordingly.

    The FAQ should be updated as well.


    PointedEars
    ___________
    [1] http://tredosoft.com/Multiple_IE
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Aug 24, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    149
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    147
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    225
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    315
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    139
    -Lost
    Apr 24, 2007
Loading...

Share This Page