In this tutorial, you will learn about String equalsIgnoreCase() method, its syntax, and usage with examples.

Java String equalsIgnoreCase() method

In Java, String equalsIgnoreCase() method takes another string as argument, and checks if the given argument string is equal to the string while ignoring the case.

When ignoring case: A and a are equal, A and A are equal, and a and a are equal.

Syntax of equals()

The syntax to call String equalsIgnoreCase() method in Java is

</>
Copy
string.equalsIgnoreCase(anotherString)

equalsIgnoreCase() has a single parameter.

ParameterDescription
anotherString[Mandatory]
A string type value.
The value which is checked with the string for equality, ignoring the case of characters in the strings.

equalsIgnoreCase() returns value of boolean type.

Examples

1. equalsIgnoreCase() – The string is equal to the another string, ignoring case

In this example, we take two strings: string and anotherString; and check if the string is equal the anotherString while ignoring the case, using String equalsIgnoreCase() method. We have taken the string values such that string equals anotherString when ignoring the case of the characters.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String string = "Apple";
		String anotherString = "APPLE";
		
		if (string.equalsIgnoreCase(anotherString)) {
			System.out.println("Given strings are EQUAL ignoring case.");
		} else {
			System.out.println("Given strings are NOT EQUAL ignoring case.");
		}
	}
}

Output

Given strings are EQUAL ignoring case.

Since string equals anotherString while ignoring the case, the equalsIgnoreCase() method returns true, and the if-block is run.

2. equalsIgnoreCase() – The string is not equal to the another string

In this example, we take two strings: string and anotherString such that the value in string variable is not equal to the value in anotherString even when ignoring the case.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String string = "Apple";
		String anotherString = "Banana";
		
		if (string.equalsIgnoreCase(anotherString)) {
			System.out.println("Given strings are EQUAL ignoring case.");
		} else {
			System.out.println("Given strings are NOT EQUAL ignoring case.");
		}
	}
}

Output

Given strings are NOT EQUAL ignoring case.

Since string does not equal anotherString, the equalsIgnoreCase() method returns false, and the else-block is run.

Conclusion

In this Java String Methods tutorial, we have seen about String equalsIgnoreCase() method in Java, its syntax, and how to use String equals() method in Java programs with the help of examples.