aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) 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 » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "String.trim() method" Watch "String.trim() method" New topic
Author

String.trim() method

Madhu Surya
Greenhorn

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 ";
System.out.println(s.length());
s.trim();
s = s + "Exam";
System.out.println(s);
}

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
    
    1

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



trim

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.

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

Joined: Aug 31, 2004
Posts: 11343

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String.trim() method