Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet56/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   52   53   54   55   56   57   58   59   ...   245
Bog'liq
Java 17 Recipes

 Solution
Use the toUpperCase() and toLowerCase() methods. The String object provides these 
two helper methods to change the case of all the characters in a given string.
For example, given the string in the following code, each of the two methods is called.
String str = "This String will change case.";
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
The following output is 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 applies to calling the toLowerCase() 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 
wish to use. Calling these methods without arguments results in a case conversion using 
the default locale. If you want to use a different locale, you can pass the desired 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 use the following code.
System.out.println(str.toUpperCase(Locale.ITALIAN));
System.out.println(str.toUpperCase(new Locale("it","US")));
System.out.println(str.toLowerCase(new Locale("fr", "CA")));
The following is the output is.
THIS STRING WILL CHANGE CASE.
THIS STRING WILL CHANGE CASE.
this string will change case.
Chapter 3 StringS


91
Converting strings to uppercase or lowercase using these methods can make life 
easy. They are also very useful for comparing strings that are taken as input from an 
application. Consider the case in which a user is prompted to enter a username, and the 
result is saved into a string. 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 toUpperCase() 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.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   52   53   54   55   56   57   58   59   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling