Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.trim() method

 
Madhu Surya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic