Java String equalsIgnoreCase Method

The Java equalsIgnoreCase method compares a string with a user-specified one to check whether they both represent the same sequence of characters or not. Based on the result, it will return Boolean True or False. Remember, this Java equalsIgnoreCase method will ignore the case differences (Case sensitivity) while comparing two strings.

This article will show how to write the equalsIgnoreCase method in the Java Programming language with an example. The syntax of the String equalsIgnoreCase is shown below.

Java String equalsIgnoreCase Method syntax

The following Java equalsIgnoreCase method accepts the string data as the argument. Next, it performs a case-insensitive comparison of the existing one with this new string to check whether they are equal.

public boolean equalsIgnoreCase(String another_String); // It returns boolean True or False

//In order to use in program
String_Object.equalsIgnoreCase(String another_String);
  • String_Object: Please specify the valid one. It will compare against the another_String
  • another_String: Please specify the valid string. It is the one we are going to compare with String_Object.

Java String equalsIgnoreCase Example

Here, we use the Java equalsIgnoreCase to check whether this string and the user-specified one are equal or not.

The first Java equalsIgnoreCase statement will call the public boolean equals (Object Obj) method to compare the string str1 with str2. From the screenshot below, observe that a is returning False because they differ in Case.

The second statement b will call the equalsIgnorecase method to perform a case-insensitive comparison on strings str1 and str2. From the above Java screenshot, see that it returns TRUE because they both are equal.

For e, we checked for non-equivalent values using the equalsIgnoreCase. Here “Tutorial Gateway” is not equal to “Java Program”.

Lastly, we used the System.out.println statements to print the output.

package StringFunctions;

public class EqualsIgnoreCaseMethod {
	public static void main(String[] args) {
		String str1 = "Tutorial GateWay";
		String str2 = "TUTORIAL GATEWAY";
		String str3 = new String("TUTORIAL Gateway");
		String str4 = new String("Tutorial GATEWAY");
		String str5 = new String("Java Programming");

		boolean a = str1.equals(str2);
		boolean b = str3.equalsIgnoreCase(str4);
		
		boolean c = str2.equalsIgnoreCase(str4);
		boolean d = str1.equalsIgnoreCase(str3);
		
		boolean e = str1.equalsIgnoreCase(str5);
		
		System.out.println("Does the String Object str1 Equals to str2? = " + a);
		System.out.println("Ignore Case: Does the String Object str1 Equals to str2? = " + b);
		System.out.println("Does the String Object str2 Equals to str4? = " + c);
		System.out.println("Does the String Object str1 Equals to str3? = " + d);
		System.out.println("Does the String Object str1 Equals to str5? = " + e);
	}
}
Java String equalsIgnoreCase Method 1

Java equalsIgnoreCase Example 2

In this Java program, we will ask the user to enter any String. Based on the user entered text, it displays the message.

The first statement will ask the user to enter any word. Then we are going to assign the user entered value to variable str.

Next, we used the If Else Statement to compare the user entered one with the String_Object “Tutorial gateway” and check whether they are equal or not.

  • If the statement inside the If is True, System.out.println(“Welcome to tutorialgateway.org”); statement printed.
  • Else System.out.println(“Goodbye to Tutorial Gateway”); statement printed.
package StringFunctions;
import java.util.Scanner;

public class EqualsIgnoreCaseMethodex {
 private static Scanner sc;
 public static void main(String[] args) {
 sc = new Scanner(System.in); 
 String str = new String("Tutorial gateway");
 System.out.println("Please Enter any word: ");
 String str1 = sc.nextLine();
 
 if (str1.equalsIgnoreCase(str)) {
 System.out.println("Welcome to tutorialgateway.org");
 }
 else {
 System.out.println("Goodbye to Tutorial Gateway");
 }
 }
}
Please Enter any word: 
TUTORIAL GATEWAY
Welcome to tutorialgateway.org

Let us enter a different word.

Please Enter any word: 
PY TUTORIAL
Goodbye to Tutorial Gateway