Re: How to trim a String trailing spaces, but not leading spaces?

Discussion in 'Java' started by Roedy Green, Sep 13, 2008.

  1. Roedy Green

    Roedy Green Guest

    On Thu, 11 Sep 2008 13:03:29 -0400, www <> wrote, quoted
    or indirectly quoted someone who said :

    >
    >The String method trim() will removed both leading and trailing spaces.
    >I want to trim only the trailing ones. Is there an existing method to do
    >that or I have to write the method myself?


    see StringTools.trimLeading

    http://mindprod.com/products1.html#COMMON11
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 13, 2008
    #1
    1. Advertising

  2. Roedy Green

    Daniel Pitts Guest

    Lew wrote:
    > Roedy Green wrote:
    >> On Thu, 11 Sep 2008 13:03:29 -0400, www <> wrote, quoted
    >> or indirectly quoted someone who said :
    >>
    >>> The String method trim() will removed both leading and trailing
    >>> spaces. I want to trim only the trailing ones. Is there an existing
    >>> method to do that or I have to write the method myself?

    >>
    >> see StringTools.trimLeading
    >>
    >> http://mindprod.com/products1.html#COMMON11

    >
    > Or you could use a pre-built and widely-used library:
    > <http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringUtils.html#stripEnd(java.lang.String,%20java.lang.String)>
    >
    >
    > It's not my personal preference, but goes to show the range of choices
    > available.
    >

    I actually use apache-commons-lang a lot in the systems at my work. It
    helps that it is pulled in already because of other dependencies ;-)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Sep 13, 2008
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Sat, 13 Sep 2008 07:37:32 -0700, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >see StringTools.trimLeading
    >
    >http://mindprod.com/products1.html#COMMON11


    StringTools is a fairly small class so it won't add too much weight.
    The stripTrailing method itself has no dependencies, so you can just
    lift the source code and put it into your own class for very
    lightweight use.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 14, 2008
    #3
  4. Roedy Green

    zerg Guest

    Roedy Green wrote:
    > The stripTrailing method itself has no dependencies, so you can just
    > lift the source code and put it into your own class for very
    > lightweight use.


    If you're willing to license your own source code appropriately, at least.
     
    zerg, Sep 14, 2008
    #4
    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. Les Caudle
    Replies:
    0
    Views:
    671
    Les Caudle
    Mar 7, 2004
  2. Generic Usenet Account

    Trimming leading and trailing spaces from a string

    Generic Usenet Account, Jul 8, 2005, in forum: C++
    Replies:
    6
    Views:
    396
    Paul Groke
    Jul 13, 2005
  3. John Nagle
    Replies:
    4
    Views:
    615
    James Antill
    Aug 7, 2007
  4. John B. Matthews
    Replies:
    4
    Views:
    694
    John B. Matthews
    Sep 12, 2008
  5. johkar
    Replies:
    2
    Views:
    3,035
    Mayeul
    Dec 10, 2009
Loading...

Share This Page