Java String toLowerCase and toUpperCase methods

Java String toLowerCase and toUpperCase methods:

Strings are built-in data-type in Java. The String class provides a couple of useful string utility methods. For example, if you want to convert a string to upper case or lower case, you don’t have to write another method for it. You can use toLowerCase and toUpperCase methods to convert a string to lower case or upper case.

String is immutable in Java, i.e. if you make any change to a string variable, it will create another string. So, both of these methods will return a new string instead of changing the characters of the original string.

In this post, we will learn how to use toLowerCase and toUpperCase methods with examples. Let me show you how these methods are defined:

Definition of toUpperCase:

These methods are local sensitive, i.e. they might give different results based on the local value. These methods are defined in the String class. toUpperCase has two different variants:

public String toUpperCase()

and

public String toUpperCase(Locale locale)

This method converts all the characters in a string to uppercase and returns the new uppercase string. This method is local sensitive, i.e. it might give different results for different local configurations.

We can pass Local.English as the local value to get the correct result for local insensitive strings.

The toUpperCase() method is equivalent to toUpperCase(Locale.getDefault()), i.e. this method uses the default local.

Example of toUpperCase:

Let me show you an example of toUpperCase:

class Main {
    public static void main(String[] args) {
        String givenString = "Hello World !!";
        String upperCaseString = givenString.toUpperCase();

        System.out.println("Given string: " + givenString);
        System.out.println("Final string: " + upperCaseString);
    }
}

Here,

  • givenString is the original string.
  • We are calling toUpperCase to convert givenString to upper case. The returned string from toUpperCase is stored in the upperCaseString variable. This is another String variable.
  • It will not change givenString. The last two lines are printing the value of givenString and upperCaseString.

If you run this program, it will print:

Given string: Hello World !!
Final string: HELLO WORLD !!

toUpperCase with Local:

Let’s try it with different Local values:

import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String givenString = "Hi !!";

        System.out.println("In Turkish: " + givenString.toUpperCase(new Locale("tr")));
        System.out.println("In English: " + givenString.toUpperCase(Locale.ENGLISH));
    }
}

We are passing a Local value as the parameter to toUpperCase. The first println statement prints the uppercase string of givenString in Turkish locale and the second statement prints the uppercase string of givenString in English locale.

The uppercase string will be different for both locale:

In Turkish: Hİ !!
In English: HI !!

Definition of toLowerCase:

toLowerCase is similar to toUpperCase. The only difference is that it will convert the string to lowercase. Similar to toUpperCase, we have two variants of toLowerCase. We can pass a Local or we can use without a Local.

public String toLowerCase()

and

public String toLowerCase(Locale locale)

The return value is a string, i.e. the string in lower case. For local insensitive strings, we can use toLowerCase(Locale.ENGLISH) to get the correct value because toLowerCase is equivalent to toLowerCase(Locale.getDefault()). It might return unexpected result if the local is different.

Example of toLowerCase:

Let’s try toLowerCase with an example:

class Main {
    public static void main(String[] args) {
        String givenString = "Hello World !!";
        String lowerCaseString = givenString.toLowerCase();

        System.out.println("Given string: " + givenString);
        System.out.println("Final string: " + lowerCaseString);
    }
}

This is the same program we tried before, givenString is the original string and lowerCaseString is the result of toLowerCase on givenString.

The last two lines are printing the values of givenString and lowerCaseString.

If you run this program, it will print the below output:

Given string: Hello World !!
Final string: hello world !!

As you can see here, the original string is converted to lowercase.

Example of toLowerCase with Local:

We can pass a Local to toLowerCase. For example:

import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String givenString = "HI !!";

        System.out.println("In Turkish: " + givenString.toLowerCase(new Locale("tr")));
        System.out.println("In English: " + givenString.toLowerCase(Locale.ENGLISH));
    }
}

It will print:

In Turkish:!!
In English: hi !!

The results are different in both turkish and english.

Comparing two strings are equal or not:

We can use these methods for case insensitive comparison of two strings. For example:

import java.util.Locale;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        String firstString, secondString;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the first string: ");
        firstString = sc.nextLine();

        System.out.println("Enter the second string: ");
        secondString = sc.nextLine();

        if (firstString.toLowerCase(Locale.ENGLISH).equals(secondString.toLowerCase(Locale.ENGLISH))) {
            System.out.println("Both strings are equal");
        } else {
            System.out.println("Strings are not equal");
        }
    }
}

This program is taking two strings as inputs from the user and comparing both string by converting them to lower case. We are using Local.ENGLISH for the comparison.

You can also use toUpperCase instead of toLowerCase here.

If you run this program, it will print output as like below:

Enter the first string: 
hello WorLd
Enter the second string: 
HELLO WORLD
Both strings are equal

Java string toUpperCase and toLowerCase examples

Reference:

You might also like: