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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 18, 2023 (edit).