Search code examples
javastringtrim

String.trim() removes not only spaces in Java


String.trim() in java removes all characters whose ascii value is less than or equal to 20 (space).

Any idea why Java did that instead of removing only space (ascii char 20)

public String trim() {
    int len = count;
    int st = 0;
    int off = offset;      /* avoid getfield opcode */
    char[] val = value;    /* avoid getfield opcode */

    while ((st < len) && (val[off + st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[off + len - 1] <= ' ')) {
         len--;
    }
    return ((st > 0) || (len < count)) ? substring(st, len) : this;
}

Solution

  • Because there are many different ways of having empty space, besides that " " space character. Quoting the javadoc:

    Returns a copy of the string, with leading and trailing whitespace omitted.

    The javadoc is clear here: it is not about space but white space. Things that would show up as "empty" - but that are in fact different from plain " " empty strings.

    In other words: this is a convenience method. Such methods are designed to provide that functionality that users do need/expect.

    It would be absolutely counter-intuition to provide a trim() method that only works spaces.

    A very typical scenario is: you receive some string. It could be entered by a user, it could be read from a file and represent a whole line. You are not interested in any trailing tabs, spaces, new line characters. Thus the fathers of the Java language give you a method to get rid of all these different characters easily. Instead of you calling trimSpaces(), trimTabs(), trimNewLines(), etc. pp.