User Oleksandr Miadelets
Oleksandr Miadelets
Head of Developers Team at CodeGym

String equalsIgnoreCase() Method in Java

Published in the Java Developer group
The equalsIgnoreCase() method in Java is used to compare two strings while ignoring the case differences (lower and upper). Like the equals method, it compares the contents of both strings. If the contents are the same it will return true, otherwise false.

Usage

When you need to compare the contents of a string with another string ignoring case sensitivity.

Syntax


inputString1.equalsIgnoreCase(inputString2);
Here the contents of both strings inputString1 and inputString2 are compared with each other.

Parameter

This method receives a string which is compared with another string calling the equalsIgnoreCase() method.

Return

The equalsIgnoreCase() method returns a boolean value. If the argument is not null and the contents are the same, ignoring the case, it returns true. Otherwise false.

Example

Let’s have a look at the Java Program to illustrate the equalsIgnoreCase() method.

public class EqualsIgnoreCaseExample {

	public static void main(String[] args)
	{
		String comparisonString1 = "Hello! I am A string";
		String comparisonString2 = "heLLo! i AM a STRING";
		String comparisonString3 = "Hey there, here's another string to compare the first two.";

		System.out.println("Comparison Results");
		
		// see if first 2 comparison strings are equal
		boolean result1 = comparisonString2.equalsIgnoreCase(comparisonString1);
		System.out.println("comparisonString2 is equal to comparisonString1 = " + result1);

		// check if second and third strings match when case ignored
		boolean result2 = comparisonString2.equalsIgnoreCase(comparisonString3);
		System.out.println("comparisonString2 is equal to comparisonString3 = " + result2);
		
	
		// check if first and third strings match
		boolean result3 = comparisonString3.equalsIgnoreCase(comparisonString1);
		System.out.println("comparisonString3 is equal to comparisonString1 = " + result3);
	}
}
String equalsIgnoreCase Method in Java - 11

Output

Comparison Results comparisonString2 is equal to comparisonString1 = true comparisonString2 is equal to comparisonString3 = false comparisonString3 is equal to comparisonString1 = false

Conclusion

We hope by now you understand how to compare two strings without taking case sensitivity into consideration. You can always use the String equalsIgnoreCase() Method in Java. Feel free to practice and get back here whenever you need more assistance. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION