It's not a secret anymore!
The moose likes Junior/Associate Certification (SCJA,OCAJ 5/6/8) and the fly likes String.trim() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Junior/Associate Certification (SCJA,OCAJ 5/6/8)
Bookmark "String.trim() method" Watch "String.trim() method" New topic

String.trim() method

Madhu Surya

Joined: Feb 10, 2007
Posts: 3
Can anyone tell me which one is correct? I have started learning a,b,c of java programming.

I tried the following code snippet

public class StringCheck {
public static void main(String args[]) {
String s = "SCJA ";
s = s + "Exam";

The o/p of the code is SCJA Exam. Trim() method of String suppose to the remove the trailing spaces at the end of the sting. But it isn't the case here, I tried to run this code in Eclipse.

Any clarification would be greatly appreciated.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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:

Code Snippet 1:

Code Snippet 2:

Here's the definition from Sun's Site

JavaDoc for java.lang.String, the Trim method


public String trim()

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.

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 ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Cameron W. McKenzie: 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
I agree. Here's the link:
subject: String.trim() method
jQuery in Action, 3rd edition