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

Discussion in 'Javascript' started by FAQ server, Dec 23, 2006.

  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=new Function("return this.replace(/^\\s+/,'')")
    String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
    String.prototype.Trim=
    new 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://www.jibbering.com/faq/.
    The FAQ workers are a group of volunteers.
    FAQ server, Dec 23, 2006
    #1
    1. Advertising

  2. FAQ server

    Evertjan. Guest

    FAQ server wrote on 23 dec 2006 in comp.lang.javascript:

    > String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")
    > String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
    >


    The bracket notation gives me a better prototyping "feel":

    String.prototype['LTrim'] =
    new Function("return this.replace(/^\\s+/,'')");

    String.prototype['RTrim'] =
    new Function("return this.replace(/\\s+$/,'')");

    String.prototype['Trim'] =
    new Function("return this.LTrim().RTrim()");

    Could be just a personal thing.




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 23, 2006
    #2
    1. Advertising

  3. In comp.lang.javascript message
    <Xns98A2665A7F846eejj99@194.109.133.242>, Sat, 23 Dec 2006 09:03:42,
    Evertjan. <> posted:
    >FAQ server wrote on 23 dec 2006 in comp.lang.javascript:
    >
    >> String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")


    >The bracket notation gives me a better prototyping "feel":
    >
    >String.prototype['LTrim'] =
    > new Function("return this.replace(/^\\s+/,'')");


    >Could be just a personal thing.


    Then what "feel" does the following give?

    String['prototype']['LTrim'] =
    new Function("return this.replace(/^\\s+/,'')");


    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
    Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
    Dr J R Stockton, Dec 24, 2006
    #3
  4. FAQ server

    Evertjan. Guest

    Dr J R Stockton wrote on 24 dec 2006 in comp.lang.javascript:

    > In comp.lang.javascript message
    > <Xns98A2665A7F846eejj99@194.109.133.242>, Sat, 23 Dec 2006 09:03:42,
    > Evertjan. <> posted:
    >>FAQ server wrote on 23 dec 2006 in comp.lang.javascript:
    >>
    >>> String.prototype.LTrim=new Function("return
    >>> this.replace(/^\\s+/,'')")

    >
    >>The bracket notation gives me a better prototyping "feel":
    >>
    >>String.prototype['LTrim'] =
    >> new Function("return this.replace(/^\\s+/,'')");

    >
    > >Could be just a personal thing.

    >
    > Then what "feel" does the following give?
    >
    > String['prototype']['LTrim'] =
    > new Function("return this.replace(/^\\s+/,'')");


    Nothing. Why are you interested?

    the nice thing about:

    >> String.prototype['LTrim'] =


    is twofold:

    1 it shows 'LTrim' a a new concept,
    while .LTrim gives an idea that it already exists.
    In my gut feeling, that is.

    2 is shows the makeability in a function like this:

    function makeDeletor(theName,theRegex){
    String.prototype[theName] =
    new Function("return this.replace(/"+theRegex+"/,'')");
    };

    makeDeletor('LTrim','^\\s+')
    makeDeletor('RTrim','\\s+$')

    // Test:
    alert( '=' + ' qwe '.LTrim().RTrim() + '=' )


    Look, no eval() !!


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 24, 2006
    #4
  5. FAQ server

    Jonas Raoni Guest

    Evertjan. escreveu:
    > the nice thing about:
    >
    >>> String.prototype['LTrim'] =

    >
    > is twofold:
    >
    > 1 it shows 'LTrim' a a new concept,
    > while .LTrim gives an idea that it already exists.
    > In my gut feeling, that is.


    Ah, I don't share the same feeling, since "String.prototype['LTrim']"
    can also be something that already exists "a += c", so for me it's
    just 3 useless characters, they don't damage and they don't add nothing
    either =]

    > 2 is shows the makeability in a function like this:
    >
    > function makeDeletor(theName,theRegex){
    > String.prototype[theName] =
    > new Function("return this.replace(/"+theRegex+"/,'')");
    > };


    Well, I think it's strange to use the brackets just to "explain" this
    kind of function. But as it's not wrong, there's nothing to talk about,
    it's just your personal preference, I have some preferences that people
    don't like haha =]

    > makeDeletor('LTrim','^\\s+')
    > makeDeletor('RTrim','\\s+$')
    >
    > // Test:
    > alert( '=' + ' qwe '.LTrim().RTrim() + '=' )
    > Look, no eval() !!


    Personally I don't see much difference (about achieving a determined
    objective) between the following two lines:

    eval("window");
    new Function("return window")();

    Also, I'm not against the use of them, everything has a right place to
    be used, the more tools we have, the best we do our job.


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
    Jonas Raoni, Dec 25, 2006
    #5
  6. FAQ server

    Randy Webb Guest

    Jonas Raoni said the following on 12/24/2006 7:48 PM:
    > Evertjan. escreveu:
    >> the nice thing about:
    >>
    >>>> String.prototype['LTrim'] =

    >>
    >> is twofold:
    >>
    >> 1 it shows 'LTrim' a a new concept, while .LTrim gives an idea that it
    >> already exists.
    >> In my gut feeling, that is.

    >
    > Ah, I don't share the same feeling, since "String.prototype['LTrim']"
    > can also be something that already exists "a += c", so for me it's
    > just 3 useless characters, they don't damage and they don't add nothing
    > either =]


    The advantage of String.prototype['some-Property'] should be self
    evident though. The difference doesn't lie in a personal perception, it
    lies in the strength of one over the other.

    <snip>

    > Personally I don't see much difference (about achieving a determined
    > objective) between the following two lines:
    > eval("window");
    > new Function("return window")();
    > Also, I'm not against the use of them, everything has a right place to
    > be used, the more tools we have, the best we do our job.


    Neither of the above lines of code help do a job better though.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Dec 25, 2006
    #6
  7. FAQ server

    Jonas Raoni Guest

    Randy Webb escreveu:
    > The advantage of String.prototype['some-Property'] should be self
    > evident though. The difference doesn't lie in a personal perception, it
    > lies in the strength of one over the other.


    Most of the people here knows the advantages of "a", I'm just talking
    about personal preferences... So, if it's not required, I prefer the
    "dot" way, I don't see any reason for changing my codes to the brackets way.

    >> eval("window");
    >> new Function("return window")();

    >
    > Neither of the above lines of code help do a job better though.


    It's not wrong to do this (before you say anything, I don't do it...):

    eval("a." + b + " = c");

    In my opinion it's not wrong, just a poor solution, since you can
    achieve the same objective using simple statements. If you don't have
    nice math skills you'll calculate something in a linear way, but if you
    know some formulas, you'll use them, the linear way isn't wrong, it's
    just a poor solution like using "eval".

    Anyway, suppose that you have code that should be parsed (the user
    enters with code, you load parts of the code through dynamic requests,
    whatever), is it wrong to use the "eval"? So I keep saying that it can
    help, things become useful when you need them.


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
    Jonas Raoni, Dec 25, 2006
    #7
    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:
    133
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    128
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    26
    Views:
    285
    Dr J R Stockton
    Feb 26, 2007
  4. FAQ server
    Replies:
    2
    Views:
    127
    -Lost
    Apr 24, 2007
  5. FAQ server
    Replies:
    0
    Views:
    90
    FAQ server
    Jun 24, 2007
Loading...

Share This Page