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.
With the methods toUpperCase()
and toLowerCase()
, we change cases. The toUpperCase
method affects only lowercase letters. And toLowerCase
affects only uppercase ones.
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
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.