String Methods

Upper and Lower Case

🙋 Need help? Ask an expert now!

The following two methods are similar to the trim() method and are built-in procedures used for making style changes to a String. Let's take a look at the results of calling one method that makes the String all lower case, and another that makes it all uppercase. The syntax for these two methods are as follows : toLowerCase() and toUpperCase().

UpperAndLowerCaseExample.java
package exlcode;

public class UpperAndLowerCaseExample {

  public static String exampleVariableOne = "Hello World!";
  // returns String with "Hello World!" either fully upper case
  // or fully lower case and assigns it to String variables
  public static String exampleVariableTwo = exampleVariableOne.toUpperCase();
  public static String exampleVariableThree = exampleVariableOne.toLowerCase();

  public static void main(String[] args) {
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
  }
}

These two methods may be useful when you want to change the String so that it will print in a particular way. On the other hand, you can use the method for personal preference or when you want to compare two strings while ignoring the differences in uppercase or lowercase. (Although compareToIgnoreCase() and equalsIgnoreCase() does exist)

Application Question

Consider the following code segment:

String varOne = "Hello World!";
            System.out.println(varOne.toLowerCase());
            

What is printed as a result of executing this code segment?