File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes trim method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "trim method" Watch "trim method" New topic
Author

trim method

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026


Execute the program like java StringTest1 alpha beta
Output:
Argbeta
Arg beta

Question: Why the trim method is not removing the trailing whitespaces in String s2.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Michael Imhof
Greenhorn

Joined: Nov 07, 2004
Posts: 24
If you're looking at the API doc for java.lang.String (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html)
you'll find the definition for the method trim():
"Returns a copy of the string, with leading and trailing whitespace omitted."

So in your example this means:



" Arg ".trim() will return a new String "Arg" and will be saved
in s1.



" Arg " + args[1] is a new String " Arg Beta" and will be saved in s2.
" Arg Beta".trim will return a new String "Arg Beta" and
will be saved in s2.

Correct output is:
ArgBeta

Arg Beta

Mike
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

this will work:

String2 you are appending string and using trim method.

String s2 = " Arg "+arg[1] ;
There is no space for triming.

class StringTest1 {
public static void main(String[] args) {
String s1 = " Arg ";
s1= s1.trim();
System.out.println(s1+ args[1] );
System.out.println();

String s2 = " Arg " ;
s2= s2.trim();
System.out.println(s2+args[1]);

}
}


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trim method
 
Similar Threads
Behaviour of String and StringBuffer class.
how many String objects are created
How many String objects are created in the code
Strings
How many String Object are created in the following code ??