SoFunction
Updated on 2025-04-07

Detailed explanation of Java case conversion and some common precautions

Preface

In Java, case conversion is a common operation in string processing. Java provides a variety of methods to implement case conversion of characters or strings. The following is a detailed explanation of case conversion in Java, including case conversion of characters, case conversion of strings, as well as some common notes and sample code.

1. Methods using String class

1.1 toLowerCase()

Converts all letters of a string to lowercase.

String str = "Hello World!";
String lowerCaseStr = ();
(lowerCaseStr);  // Output: hello world!

Features:

  • The original string will not be changed.
  • Supports locale, which can convert case according to a specific locale.

For example:

String str = "İSTANBUL";
String lowerCaseStr = (); // "istanbul"
String lowerCaseTurkish = (new Locale("tr", "TR")); // "i̇stanbul"

1.2 toUpperCase()

Converts all letters of a string to uppercase.

String str = "Hello World!";
String upperCaseStr = ();
(upperCaseStr);  // Output: HELLO WORLD!

Features:

  • Regionalization is also supported.
  • Non-alphabetical characters such as numbers and symbols remain unchanged.

2. Handle the case of a single character

Java providesCharacterClass, used to handle case of a single character.

2.1 ()

Converts a single character to lowercase.

char c = 'A';
char lowerCaseC = (c);
(lowerCaseC);  // Output: a

2.2 ()

Converts a single character to uppercase.

char c = 'a';
char upperCaseC = (c);
(upperCaseC);  // Output: A

Example: Case conversion of character arrays

char[] chars = {'H', 'e', 'l', 'l', 'o'};
for (int i = 0; i < ; i++) {
    chars[i] = (chars[i]);
}
(chars);  // Output: hello

3. Custom case conversion

If more flexible case conversion is needed (such as converting only partial characters or converting according to specific rules), you can use a loop combinationCharacterClass implementation.

Example: First letter uppercase, other lowercase

public class Main {
    public static void main(String[] args) {
        String str = "hElLo wOrLd!";
        String[] words = (" ");
        StringBuilder result = new StringBuilder();

        for (String word : words) {
            String capitalized = ((0)) + (1).toLowerCase();
            (capitalized).append(" ");
        }
        (().trim());  // Output: Hello World!    }
}

4. Things to note in case conversion

  • Non-alphabetical characters remain unchanged

    • Case conversion affects only alphabetical characters (a-z and A-Z). Non-alphabetical characters (such as numbers, spaces, symbols) will not be affected.
    String str = "123!ABC";
    (());  // Output: 123!abc
  • Unicode Support

    • Java's case conversion is based on the Unicode standard and can handle international characters correctly.
    String str = "Straße";  // "ß" in German(());  // Output: STRASSE
  • Locale

    • Different languages ​​may have specific rules for case conversion. For example, in Turkish,IThe lowercase form isı(rather thani)。
    String turkish = "I";
    ((("tr")));  // Output: ı
  • performance

    • For large strings, frequently calledtoLowerCase()ortoUpperCase()May affect performance. Minimize unnecessary conversion operations.

5. Batch case conversion

If you need to case conversion of all strings in a collection (such as lists or arrays), you can use the Stream API or loop implementation.

Example: All strings in the conversion list are lowercase

import .*;
import ;

public class Main {
    public static void main(String[] args) {
        List<String> words = ("HELLO", "WORLD", "JAVA");
        List<String> lowerCaseWords = ()
                                           .map(String::toLowerCase)
                                           .collect(());
        (lowerCaseWords);  // Output: [hello, world, java]    }
}

6. Frequently Asked Questions

Q1: How to convert only some strings?

usesubstring()Methods intercept strings, convert them into upper and lower cases and then splice them.

String str = "helloWorld";
String part1 = (0, 5).toUpperCase(); // HELLO
String part2 = (5).toLowerCase();    // world
(part1 + part2);  // Output: HELLOworld

Q2: How to determine whether characters are uppercase or lowercase?

use()and()

char c = 'A';
if ((c)) {
    (c + "is capital letters");
}

char d = 'a';
if ((d)) {
    (d + "It's lowercase letters");
}

7. Complete case: String case inversion

Inverts the case of each letter in the string.

public class Main {
    public static void main(String[] args) {
        String str = "Hello World!";
        StringBuilder result = new StringBuilder();

        for (char c : ()) {
            if ((c)) {
                ((c));
            } else if ((c)) {
                ((c));
            } else {
                (c);  // Non-alphabetical characters remain unchanged            }
        }
        (());  // Output: hELLO wORLD!    }
}

Summarize

Java provides built-inStringandCharacterMethod, can easily implement case conversion. By flexibly applying these methods, you can handle case conversion scenarios of various characters and strings, such as global conversion, partial conversion, initial case, inverse case, etc. At the same time, pay attention to internationalization and performance issues to ensure code robustness and efficiency.

This is the end of this article about Java case conversion and some common precautions. For more detailed explanations of Java case conversion, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!