The toUpperCase()
method is a member of the java.lang.String
class. The toUpperCase() method transforms a string by replacing all lowercase characters with their corresponding uppercase counterparts, while leaving any characters that are already in uppercase unaffected.
See Also: String toLowerCase() Method
1. String.toUpperCase() Method
The toUpperCase() method is an overloaded method. It takes an optional argument of type Locale.
- When Locale information is passed, the method returns a new String after converting all the alphabetic characters to uppercase according to the specified Locale rules.
- When Locale information is NOT passed, the default locale rules are applied.
public String toUpperCase() //Uses default Locale rules
public String toUpperCase(Locale locale) //Optional Locale information
Let us start with a very simple example to understand the usage of toUpperCase() method.
String original = "Hello, World!";
String upperCase = original.toUpperCase();
//String upperCase = original.toUpperCase(Locale.US); //Optional Locale information
System.out.println(upperCase); // Output: HELLO, WORLD!
As demonstrated above, the toUpperCase()
method converts all letters in the string to uppercase, making it a handy tool for various string manipulation tasks.
The toUppercase() method is equal to calling the toUpperCase(Locale.getDefault()) method that uses the current Locale rules.
2. String.toUpperCase() with Custom Locale
The following Java program converts a string to uppercase using the Locale.US. When you run this code, it will convert the Greek string “Γειά σου Κόσμε” to uppercase using the rules of the United States locale.
String uppercaseString = "Γειά σου Κόσμε".toUpperCase(Locale.US);
System.out.println(uppercaseString);
The output will be:
ΓΕΙΆ ΣΟΥ ΚΌΣΜΕ
3. Usages of toUpperCase() Method
We can use the upper case strings in many real-world problems during data processing.
For example, when we need to sort a list of strings case-insensitively, we can use toUpperCase()
in the sorting Comparator. This ensures that the sorting is done without considering the letter case.
List<String> names = Arrays.asList("alice", "Bob", "charlie", "dave");
Collections.sort(names, (s1, s2) -> s1.toUpperCase().compareTo(s2.toUpperCase()));
System.out.println(names); // Output: [alice, Bob, charlie, dave]
Similarly, we can use toUpperCase() to perform case-insensitive string searches. We can convert both the search term and the target string to uppercase. Using toUpperCase()
ensures that the search is not case-sensitive.
String text = "Java is a powerful programming language";
String searchTerm = "java";
if (text.toUpperCase().contains(searchTerm.toUpperCase())) {
System.out.println("Match found!");
} else {
System.out.println("Match not found.");
}
4. Length of the Original and Uppercase Strings can be Unequal
Note that the length of the original string and upper string may be different due to a few locale-specific characters.
In this example, we have a string "straße"
, which means “street” in German. When we convert it to uppercase using the German locale (Locale.GERMAN
), the ‘ß’ character transforms into ‘SS’.
String originalText = "straße"; // German word for "street"
String uppercaseText = originalText.toUpperCase(Locale.GERMAN); // Using the German locale
System.out.println("Original Text: " + originalText);
System.out.println("Uppercase Text: " + uppercaseText);
System.out.println("Original Length: " + originalText.length());
System.out.println("Uppercase Length: " + uppercaseText.length());
The program output:
Original Text: straße
Uppercase Text: STRASSE
Original Length: 6
Uppercase Length: 7
So, when working with the toLowerCase()
method in Java and considering locale-specific characters, it’s essential to be aware that the length of the resulting string may not always be the same.
Happy Learning !!
References: String Java Doc
Comments