Java Reference
In-Depth Information
System.out.println(str.toLowerCase(new Locale("fr",
"CA")));
Converting strings to upper- or lowercase using these methods can make life easy.
They are also very useful for comparing strings that are taken as input from an applica-
tion. Consider the case in which a user is prompted to enter a username, and the result
is saved into a string. Now consider that later in the program that string is compared
against all the usernames stored within a database to ensure that the username is valid.
What happens if the person who entered the username types it with an uppercase first
character? What happens if the username is stored within the database in all uppercase?
The comparison will never be equal. In such a case, a developer can use the toUp-
perCase() method to alleviate the problem. Calling this method against the strings
that are being compared will result in a comparison in which the case is the same in
both strings.
3-5. Concatenating Strings
Problem
There are various strings that you want to combine into one.
Solution #1
If you want to concatenate strings onto the end of each other, use the concat() meth-
od. The following example demonstrates the use of the concat() method:
String one = "Hello";
String two = "Java8";
String result = one.concat(" ".concat(two));
The result is this:
Hello Java8
Solution #2
Search WWH ::




Custom Search