Changing the Case of Characters Within a String


In Java, there are two methods for changing the casing of all the characters in a string:

  1. The toLowerCase() method converts all the characters in a string from uppercase to lowercase.
  2. The toUpperCase() method converts all the characters in a string from lowercase to uppercase.

Nonalphabetical characters, such as digits, are unaffected. Here are the simplest form of these methods:

String toLowerCase()
String toUpperCase()

Both methods return a string object that contains uppercase or lowercase letters equal to the string. The default locale handles the conversion in both cases.

Example of toUpperCase()

Program
// Demonstrates toUpperCase()
public class Main 
{
  public static void main(String[] args) 
  {
    String str = "Hello World";
    String UpperCasestr = str.toUpperCase();
    System.out.println("Normal String : " + str);
    System.out.println("Using toUpperCase() : " + UpperCasestr);
  }
}

Example of toLowerCase()

Program
// Demonstrates toLowerCase()
public class Main 
{
  public static void main(String[] args) 
  {
    String str = "Hello World";
    String LowerCasestr = str.toLowerCase();
    System.out.println("Normal String : " + str);
    System.out.println("Using toLowerCase() : " + LowerCasestr);
  }
}