Java String equalsIgnoreCase Method

The Java String equalsIgnoreCase method compares a string with user-specified string 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, while comparing two strings, this Java qualsIgnoreCase method will ignore the case differences (Case sensitivity).

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

Java String equalsIgnoreCase Method syntax

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

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

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

Java String equalsIgnoreCase Example

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

// Java String.equalsIgnoreCase example
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

The following Java String equalsIgnoreCase statement will call the public boolean equals (Object Obj) method to compare the string str1 with str2. From the above screenshot, observe that it is returning False because they are different in Case.

boolean a = str1.equals(str2);

It will call the public boolean equalsIgnorecase (String another_String) method to perform a case-insensitive comparison on string str1 and str2. From the above Java screenshot, see that it is returning TRUE because they both are equal.

boolean b = str3.equalsIgnoreCase(str4);

Next, we checked for non-equivalent values using the equalsIgnoreCase. Here “Tutorial GateWay” is not at all equals to “Java Program”.

boolean e = str1.equalsIgnoreCase(str5);

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

Java equalsIgnoreCase Example 2

In this Java program, We are going to ask the user to enter any String. Based on the user entered string value, it displays the message.

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 different word

Please Enter any word: 
java TUTORIAL
Goodbye to Tutorial Gateway

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

System.out.println("Please Enter any word: ");
String str1 = sc.nextLine();

Next, we used the Java If Else Statement to compare the user entered string 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.