The trim() method of the String class is effective at trimming off both leading and trailing whitespace. However, the name of the method is misleading, as it doesn't trim the length of the String on which it is invoked, but instead, returns a new String with the length trimmed. The immutability of the String class pretty much forces this I guess.
So, with trim(), the String on which the method is called is left alone, but a new String is returned.
Here's a quick snippet from the SCJA Certification Guide:
When character Strings are fed from a user interface, into a programming model, the Strings are often filled with all sorts of trailing whitespace, also known as blank characters, that serve no purpose, and only take up space in memory, and on the hard drive when physically stored. The trim() method creates a new String, with all leading and trailing whitespace removed and getting rid of redundant whitespace will make your programs more efficient.
It is important to note that the trim() method trims off both leading and trailing whitespace, and it returns a new, trimmed String, without changing the String on which the method is originally invoked.
From your original code, I believe the following two code snippets would produce the results in which you are interested:
Returns a copy of the string, with leading and trailing whitespace omitted.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.
Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).
This method may be used to trim whitespace (as defined above) from the beginning and end of a string.
Returns: A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
-Cameron McKenzie [ March 11, 2007: Message edited by: Cameron W. McKenzie ]
Originally posted by Cameron W. McKenzie: ...it doesn't trim the length of the String on which it is invoked, but instead, returns a new String with the length trimmed. The immutability of the String class pretty much forces this I guess...
Right. Strings are immutable, so you're never really modifying a String object. Instead, methods like this create a new String and return a reference to the new String.
So when invoking these methods, you need to assign that return value to some variable (often the same variable that referenced the original String). Otherwise, the new String is created, but you will have no way to reach it. [ March 12, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org