Java toLowerCase, toUpperCase Examples

This Java example uses the toLowerCase and toUpperCase methods to modify the character casing in strings.
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.

Java program that changes casing 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); } } Output THE GOLDEN BOWL the golden bowl
Notes, above example. As mentioned the space character in the string "The Golden Bowl" is not changed. You cannot uppercase a space.
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.HashMap
A summary. As with other string manipulation methods, toUpperCase and toLowerCase do not modify an existing string. Instead they copy and return new strings.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls