Java String toLowerCase() & toUpperCase(): Convert Text Case with Locale Awareness
1. toLowerCase()
The toLowerCase() method transforms every character of a string into its lower‑case equivalent using the default locale’s rules. Because it is locale‑sensitive, results can differ for characters like the German "ß" or Turkish "İ" when the default locale is not appropriate.
For situations that require a specific language, you can call the overloaded variant:
String lower = original.toLowerCase(Locale.forLanguageTag("de"));
Key points:
- Returns a new
String; the original remains unchanged. - No parameters in the no‑arg form.
- Useful for case‑insensitive comparisons and normalizing user input.
Example 1:
public class Guru99 {
public static void main(String[] args) {
String s1 = new String("UPPERCASE CONVERTED TO LOWERCASE");
System.out.println(s1.toLowerCase());
}
}
Output: uppercase converted to lowercase
2. toUpperCase()
The toUpperCase() method converts all characters of a string to upper‑case using the default locale. As with toLowerCase(), it is locale‑sensitive, so be cautious when handling strings that contain locale‑specific characters.
For explicit control, use the locale‑aware overload:
String upper = original.toUpperCase(Locale.forLanguageTag("tr"));
Key points:
- Produces a new
String; the source string is unchanged. - No parameters in the no‑arg form.
- Commonly used for formatting output or preparing keys for case‑insensitive storage.
Example 2:
public class Guru99 {
public static void main(String[] args) {
String s1 = new String("lowercase converted to uppercase");
System.out.println(s1.toUpperCase());
}
}
Output: LOWERCASE CONVERTED TO UPPERCASE
Java
- Java Variables and Literals: A Comprehensive Guide
- Java Methods: How to Define, Call, and Use Them Effectively
- Mastering Java Strings: Creation, Methods, and Best Practices
- Java Abstract Classes and Methods: A Comprehensive Guide
- Mastering String Representations in Java Enums
- Mastering Java StringWriter: Usage, Methods, and Practical Examples
- Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
- Mastering Java String Replacement: replace(), replaceAll(), and replaceFirst() Explained
- Java 8 Overview: New Functional, Streaming, and Date-Time APIs
- Java 8 Default Methods Explained: Enhancing Interfaces & Backward Compatibility