Java Reference
In-Depth Information
String myString = " This is a String that contains
whitespace. ";
System.out.println(myString);
System.out.println(myString.trim());
The output will print as follows:
This is a String that contains whitespace.
This is a String that contains whitespace.
How It Works
Regardless of how careful we are, whitespace is always an issue when working with
strings of text. This is especially the case when comparing strings against matching
values. If a string contains an unexpected whitespace character then that could be dis-
astrous for a pattern-searching program. Luckily, the Java String object contains the
trim() method that can be used to automatically remove whitespace from each end
of any given string.
The trim() method is very easy to use. In fact, as you can see from the solution
to this recipe, all that is required to use the trim() method is a call against any given
string. Because strings are objects, they contain many helper methods, which can make
them very easy to work with. After all, strings are one of the most commonly used data
types in any programming language . . . so they'd better be easy to use! The trim()
method returns a copy of the original string with all leading and trailing whitespace re-
moved. If, however, there is no whitespace to be removed, the trim() method returns
the original string instance. It does not get much easier than that!
3-4. Changing the Case of a String
Problem
A portion of your application contains case-sensitive string values. You want to change
all the strings to uppercase before they are processed in order to avoid any case-sensit-
ivity issues down the road.
Search WWH ::




Custom Search