exampracticeonline.com
String equalsIgnoreCase()

Java String equalsIgnoreCase()

The Java String equalsIgnoreCase() method checks two String equailty based on the content of both String in case insensitive manner. If both String have same charaters then equalsIgnoreCase() method returns true else returns false.

Important points:

  1. Java String equalsIgnoreCase() method is a case insensitive. Java has equals() method for case sensitive comparision.
  2. String class overrides equals() method from Object class.
  3. "==" operator compare the references but equalsIgnoreCase() method compare contant of given String.

Internal method implementation

You can see this internal implementation in String.class provided in java.lang package.

/**
     * Compares this {@code String} to another {@code String}, ignoring case
     * considerations.  Two strings are considered equal ignoring case if they
     * are of the same length and corresponding characters in the two strings
     * are equal ignoring case.
     */
    public boolean equalsIgnoreCase(String anotherString) {
        return (this == anotherString) ? true
                : (anotherString != null)
                && (anotherString.value.length == value.length)
                && regionMatches(true, 0, anotherString, 0, value.length);
    }

Method Syntax

public boolean equalsIgnoreCase(String anotherString) 

Method Parameter

anotherObject: another object that compared with given String.

Method Returns

Java String equalsIgnoreCase() method returns boolean value true or false. true if characters of both strings are equal otherwise false.

1. Example - Java String equalsIgnoreCase() method

package com.examples;

public class StringEqualsIgnoreCaseExample {
        
        public static void main(String args[]) {
                String str1 = "java";
                String str2 = "JAVA";
                String str3 = "java";
                String str4 = "python";
                System.out.println("Is java and JAVA equals ? : "+str1.equalsIgnoreCase(str2));
                System.out.println("Is java and java equals ? : "+str1.equalsIgnoreCase(str3));
                System.out.println("Is java and python equals ? : "+str1.equalsIgnoreCase(str4));
        }
        
}

Output: 

Is java and JAVA equals ? : true
Is java and java equals ? : true
Is java and python equals ? : false

2. Example - Java String equalsIgnoreCase() method

package com.examples;

public class StringEqualsIgnoreCaseExample {
        
        public static void main(String args[]) {
                String str = "Java learning";
                if(str.equalsIgnoreCase("Java learning")) {
                        System.out.println("Yes, str equals with Java learning");
                } else {
                        System.out.println("No, str not equals with Java learning");
                }
        }     
}

Output: 

Yes, str equals with Java learning

3. Example - Java String equalsIgnoreCase() method

package com.examples;

public class StringEqualsIgnoreCaseExample {
        
        public static void main(String args[]) {
                String str = "Java learning";
                if(str.equals("JAVA learning")) {
                        System.out.println("Yes, str equals with JAVA learning");
                } else {
                        System.out.println("No, str not equals with JAVA learning");
                }
        }     
}

Output: 

Yes, str equals with JAVA learning

NOTE: In equals() method it returns "No, str not equals with JAVA learning" because of case sensitive.