String toLowerCase, toUpperCase ExamplesUse the toLowerCase and toUpperCase methods to modify the character casing in strings.
This page was last reviewed on May 18, 2023.
ToLowerCase. A string has uppercase letters. But we want just lowercase letters—the toLowerCase method is useful here. We call toLowerCase on an existing String object.
With another method, toUpperCase we go from lower to uppercase. Only letters are affected by these methods. Spaces and digits are left alone.
An example. With the methods toUpperCase() and toLowerCase(), we change cases. The toUpperCase method affects only lowercase letters. And toLowerCase affects only uppercase ones.
Tip Numbers, punctuation and whitespace are unaffected by these two methods. Sometimes, the methods will make no changes.
Info The space character in the string "The Golden Bowl" is not changed. You cannot uppercase a space.
public class Program { public static void main(String[] args) { // A mixed-case string. String value1 = "The Golden Bowl"; // Change casing. String upper = value1.toUpperCase(); String lower = value1.toLowerCase(); // Print results. System.out.println(upper); System.out.println(lower); } }
THE GOLDEN BOWL the golden bowl
Notes, performance. There is a possible performance gain in storing the result of toUpperCase and toLowerCase. A HashMap could be used. You can reduce string allocations this way.
As with other string manipulation methods, toUpperCase and toLowerCase do not modify an existing string. Instead they copy and return new strings.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 18, 2023 (edit).
© 2007-2024 Sam Allen.