Java Case Conversion: Uppercase & Lowercase Methods
There are several methods for converting case in Java.
- Convert the string to uppercase using the toUpperCase() method.
String str = "HelloWorld";
String upperCaseStr = str.toUpperCase();
System.out.println(upperCaseStr); // 输出:HELLOWORLD
- Convert the string to lowercase using the toLowerCase() method.
String str = "HelloWorld";
String lowerCaseStr = str.toLowerCase();
System.out.println(lowerCaseStr); // 输出:helloworld
- Convert characters to uppercase using the toUpperCase() method of the Character class.
char c = 'a';
char upperCaseChar = Character.toUpperCase(c);
System.out.println(upperCaseChar); // 输出:A
- Convert characters to lowercase using the toLowerCase() method of the Character class.
char c = 'A';
char lowerCaseChar = Character.toLowerCase(c);
System.out.println(lowerCaseChar); // 输出:a