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

Discussion in 'Java' started by John B. Matthews, Sep 11, 2008.

  1. In article <gabj11$7l3$>, www <>
    wrote:

    [...]
    > 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?

    [...]

    Yes and no. Consider the String method replaceAll("\\s+$", ""):

    <http://java.sun.com/javase/6/docs/api/java/lang/String.html#replaceAll(j
    ava.lang.String,%20java.lang.String)>

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Sep 11, 2008
    #1
    1. Advertising

  2. John B. Matthews

    Lew Guest

    On Sep 11, 1:38 pm, "John B. Matthews" <> wrote:
    > In article <gabj11$>, www <>
    > wrote:
    >
    > [...]> 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?

    >
    > [...]
    >
    > Yes and no. Consider the String method replaceAll("\\s+$", ""):
    >
    > <http://java.sun.com/javase/6/docs/api/java/lang/String.html#replaceAll(j
    > ava.lang.String,%20java.lang.String)>


    public static String rTrim( String in )
    {
    int len = in.length();
    while ( len > 0 )
    {
    if ( ! Character.isWhitespace( in.charAt( --len ))
    {
    return in.substring( in, 0, len + 1 );
    }
    }
    return "";
    }
     
    Lew, Sep 11, 2008
    #2
    1. Advertising

  3. Recently, www <> wrote:
    [...]
    > > String method replaceAll("\\s+$", ""):

    [...]
    > I also learned a tip about regular expression.


    As did I. Sadly, they tend to be slow. Curious, I compared the regex to
    Lew's loop proposal. The latter is an order of magnitude faster. The two
    also elide different characters, although I only tested the ones in
    common:

    <http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html>
    <http://java.sun.com/javase/6/docs/api/java/lang/Character.html#isWhitesp
    ace(int)>

    <sscce>
    public class RightTrim {

    private static final String TEST = "Test \t\n\u000B\f\r";

    public static void main(String[] args) {
    (new RegEx()).test(TEST);
    (new Loop()).test(TEST);
    }
    }

    abstract class Test {
    public static final int COUNT = 100000;
    public void test(String in) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < COUNT; i++) rTrim(in);
    System.out.println(System.currentTimeMillis() - start);
    }
    public abstract String rTrim(String in);
    }

    /** @author JBM */
    class RegEx extends Test {
    public String rTrim( String in ) {
    return in.replaceAll("\\s+$", "");
    }
    }

    /** @author Lew */
    class Loop extends Test {
    public String rTrim( String in ) {
    int len = in.length();
    while ( len > 0 ) {
    if ( ! Character.isWhitespace( in.charAt( --len ))) {
    return in.substring( 0, len + 1 );
    }
    }
    return "";
    }
    }
    </sscce>

    > Thank you very much.


    My pleasure.

    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Sep 12, 2008
    #3
  4. John B. Matthews

    Mark Space Guest

    John B. Matthews wrote:

    >
    > As did I. Sadly, they tend to be slow. Curious, I compared the regex to
    > Lew's loop proposal. The latter is an order of magnitude faster. The two


    Micro benchmarks can be a dangerous thing. In your code you it's pretty
    likely that the regex search (which takes a string parameter) has to be
    compiled each time it runs. I'd be curious what the run time is if the
    pattern is precompiled once then re-used. You also only search one very
    short string, which may skew the results also.

    However, good job doing the benchmarking. It's important to test, and
    even a skewed test might be better than none at all.
     
    Mark Space, Sep 12, 2008
    #4
  5. In article <gacdhi$u9$>,
    Mark Space <> wrote:

    > John B. Matthews wrote:
    >
    > >
    > > As did I. Sadly, they tend to be slow. Curious, I compared the regex to
    > > Lew's loop proposal. The latter is an order of magnitude faster. The two

    >
    > Micro benchmarks can be a dangerous thing. In your code you it's pretty
    > likely that the regex search (which takes a string parameter) has to be
    > compiled each time it runs. I'd be curious what the run time is if the
    > pattern is precompiled once then re-used. You also only search one very
    > short string, which may skew the results also.


    Excellent point. Not surprisingly, pre-compiling the regex helps, but
    the benefit diminishes with increasing string length. The Loop time
    approaches the others to within a factor of two, but only for
    unreasonably long padding.

    <sscce>
    import java.lang.StringBuilder;
    import java.util.regex.Pattern;

    public class RightTrim {

    public static void main(String[] args) {
    for (int i = 1; i < 5; i++) {
    String s = testString((int) Math.pow(10, i));
    (new RegEx()).test(s);
    (new Compiled()).test(s);
    (new Loop()).test(s);
    System.out.println();
    }
    }

    private static String testString(int padding) {
    String controls = "\t\n\u000B\f\r";
    StringBuilder sb = new StringBuilder("Test");
    for (int i = 0; i < padding; i++) sb.append(" ");
    sb.append(controls);
    return sb.toString();
    }
    }

    abstract class Test {
    public static final int COUNT = 10000;
    public void test(String in) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < COUNT; i++) rTrim(in);
    System.out.println(name()
    + (System.currentTimeMillis() - start));
    }
    public abstract String rTrim(String in);
    public abstract String name();
    }

    /** @author JBM */
    class RegEx extends Test {
    public String rTrim( String in ) {
    return in.replaceAll("\\s+$", "");
    }
    public String name() { return "RegExpr: "; }
    }

    /** @author JBM, MS */
    class Compiled extends Test {
    private static final Pattern right = Pattern.compile("\\s+$");
    public String rTrim( String in ) {
    return right.matcher(in).replaceAll("");
    }
    public String name() { return "Compiled: "; }
    }

    /** @author Lew */
    class Loop extends Test {
    public String rTrim( String in ) {
    int len = in.length();
    while ( len > 0 ) {
    if ( ! Character.isWhitespace( in.charAt( --len ))) {
    return in.substring( 0, len + 1 );
    }
    }
    return "";
    }
    public String name() { return "Loop: "; }
    }
    </sscce>

    > However, good job doing the benchmarking. It's important to test, and
    > even a skewed test might be better than none at all.


    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Sep 12, 2008
    #5
    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. Roedy Green
    Replies:
    3
    Views:
    658
  5. johkar
    Replies:
    2
    Views:
    3,036
    Mayeul
    Dec 10, 2009
Loading...

Share This Page