Java Reference
In-Depth Information
Solution
Make use of the toUpperCase() and toLowerCase() methods. The String
object provides these two helper methods to assist in performing a case change for all
of the characters in a given string.
For example, given the string in the following code, each of the two methods will
be called:
String str = "This String will change case.";
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
The following output will be produced:
THIS STRING WILL CHANGE CASE.
this string will change case.
How It Works
To ensure that the case of every character within a given string is either upper- or
lowercase, use the toUpperCase() and toLowerCase() methods, respectively.
There are a couple of items to note when using these methods. First, if a given string
contains an uppercase letter, and the toUpperCase() method is called against it, the
uppercase letter is ignored. The same concept holds true for calling the toLower-
Case() method. Any punctuation or numbers contained within the given string are
also ignored.
There are two variations for each of these methods. One of the variations does not
accept any arguments, while the other accepts an argument pertaining to the locale you
want to use. Calling these methods without any arguments will result in a case conver-
sion using the default locale. If you want to use a different locale, you can pass the de-
sired locale as an argument, using the variation of the method that accepts an argument.
For instance, if you want to use an Italian or French locale, you would use the follow-
ing code:
System.out.println(str.toUpperCase(Locale.ITALIAN));
System.out.println(str.toUpperCase(new
Locale("it","US")));
Search WWH ::




Custom Search