String.toUpperCase() / toLowerCase() / trim()
Methods for converting strings to uppercase or lowercase, and for removing whitespace from the beginning and end of a string. Commonly used for normalizing input values and preprocessing before comparisons. Java 11 and later also provides strip() variants with full Unicode support.
Syntax
// Converts all characters in the string to uppercase. str.toUpperCase(); // Converts all characters in the string to lowercase. str.toLowerCase(); // Removes leading and trailing ASCII whitespace and control characters (before Java 11). str.trim(); // Removes all leading and trailing Unicode whitespace (Java 11 and later). str.strip(); // Removes only leading whitespace (Java 11 and later). str.stripLeading(); // Removes only trailing whitespace (Java 11 and later). str.stripTrailing();
Method List
| Method | Description |
|---|---|
| toUpperCase() | Returns a new string with all alphabetic characters converted to uppercase. |
| toLowerCase() | Returns a new string with all alphabetic characters converted to lowercase. |
| trim() | Returns a new string with leading and trailing ASCII spaces, tabs, newlines, and other control characters removed. |
| strip() | Returns a new string with leading and trailing Unicode whitespace (including full-width spaces) removed. Available in Java 11 and later. |
| stripLeading() | Returns a new string with only the leading whitespace characters removed. Available in Java 11 and later. |
| stripTrailing() | Returns a new string with only the trailing whitespace characters removed. Available in Java 11 and later. |
Sample Code
// Example of uppercase and lowercase conversion.
String str = "Hello, Java!";
System.out.println(str.toUpperCase()); // Prints "HELLO, JAVA!"
System.out.println(str.toLowerCase()); // Prints "hello, java!"
// Comparing strings in a case-insensitive way.
String input = "JAVA";
System.out.println(input.toLowerCase().equals("java")); // Prints "true"
// Using trim() to remove surrounding whitespace.
String padded = " Hello ";
System.out.println(padded.trim()); // Prints "Hello"
// strip() also handles full-width spaces (Java 11 and later).
String full = "\u3000Hello\u3000"; // \u3000 is a full-width space.
System.out.println(full.trim()); // Full-width spaces are NOT removed.
System.out.println(full.strip()); // Prints "Hello"
// Leading and trailing whitespace can be removed independently.
String text = " Hello World ";
System.out.println(text.stripLeading()); // Prints "Hello World "
System.out.println(text.stripTrailing()); // Prints " Hello World"
Notes
toUpperCase() and toLowerCase() convert the case of alphabetic characters and have no effect on non-Latin characters such as Japanese. To compare strings in a case-insensitive way, either normalize both strings to the same case before calling equals(), or use equalsIgnoreCase() directly for a more concise approach.
There are two options for stripping whitespace: trim() and strip(). trim() does not remove Unicode whitespace characters (such as full-width spaces) as defined by the Unicode standard, so it is recommended to use strip() (Java 11 and later) when processing user input.
For string comparison, see equals() / equalsIgnoreCase(). For string replacement, see replace() / replaceAll().
If you find any errors or copyright issues, please contact us.