In this tutorial, you will learn how to check if given strings are equal ignoring case of the characters in Java, using String.equalsIgnoreCase() method.

Java – Check if strings are equal ignoring case

To check if given strings are equal ignoring the case in Java, you can use the String.equalsIgnoreCase() method. Call equalsIgnoreCase() method on the first string, and pass the second string as argument.

If str1 and str2 are given strings, then the expression to check if these two strings are equal ignoring the case is

str1.equalsIgnoreCase(str2)

If the strings are equal ignoring the case, then equalsIgnoreCase() method returns a boolean value of true, else it returns false.

We can use the above expression as a condition in Java if else statement.

1. Check if strings str1 and str2 are equal ignoring case using String.equalsIgnoreCase() in Java

In this example, we take two strings in str1 and str2. We shall use String.equalsIgnoreCase() method to check if the string values in these two variables are equal ignoring the case.

Java Program

public class Main {
	public static void main(String[] args) {
		String str1 = "Apple";
		String str2 = "apple";
		
		if (str1.equalsIgnoreCase(str2)) {
			System.out.println("Strings are EQUAL ignoring case.");
		} else {
			System.out.println("Strings are NOT EQUAL ignoring case.");
		}
	}
}

Output

Strings are EQUAL ignoring case.

Now, let us take another values in the string variables, and run the program.

Java Program

public class Main {
	public static void main(String[] args) {
		String str1 = "Apple";
		String str2 = "banana";
		
		if (str1.equalsIgnoreCase(str2)) {
			System.out.println("Strings are EQUAL ignoring case.");
		} else {
			System.out.println("Strings are NOT EQUAL ignoring case.");
		}
	}
}

Output

Strings are NOT EQUAL ignoring case.
ADVERTISEMENT

Conclusion

In this Java String tutorial, we have seen how to check if the given strings are equal by ignoring the case in Java using String.equalsIgnoreCase() method, with examples.