Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

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:

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:

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

  1. Java Variables and Literals: A Comprehensive Guide
  2. Java Methods: How to Define, Call, and Use Them Effectively
  3. Mastering Java Strings: Creation, Methods, and Best Practices
  4. Java Abstract Classes and Methods: A Comprehensive Guide
  5. Mastering String Representations in Java Enums
  6. Mastering Java StringWriter: Usage, Methods, and Practical Examples
  7. Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
  8. Mastering Java String Replacement: replace(), replaceAll(), and replaceFirst() Explained
  9. Java 8 Overview: New Functional, Streaming, and Date-Time APIs
  10. Java 8 Default Methods Explained: Enhancing Interfaces & Backward Compatibility