Java String equalsIgnoreCase() Method

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Java’s equalsIgnoreCase() method is a vital string comparison function that allows case-insensitive equality checks. By ignoring differences in case, equalsIgnoreCase() can compare strings based solely on their character sequences. This method is beneficial when performing string comparisons where case does not matter, such as usernames, passwords, codes, etc.

This article will cover the syntax, parameters, return type, exceptions, examples, implementation details, and conclusions of using the equalsIgnoreCase() method for case-insensitive string comparisons in Java. This overview provides a foundation for understanding the key aspects and usage of equalsIgnoreCase().

Syntax of Java String equalsIgnoreCase()

The method signature of Java equalsIgnoreCase() is:

public boolean equalsIgnoreCase(String anotherString)

It is used as:

string1.equalsIgnoreCase(string2)

Here, string1 is the string on which this method is called, and string2 is the string to be compared with string1.

Parameters of Java String equalsIgnoreCase()

This method accepts a single parameter:

anotherString – The string to be compared with the string on which this method is called. This parameter is of String type.

For example:

str.equalsIgnoreCase(anotherString);

Here, str is the string on which equalsIgnoreCase() is called and anotherString is the parameter.

Return Type of Java equalsIgnoreCase() Method

The equalsIgnoreCase() method returns a boolean value:

  • It returns true if the corresponding characters of both strings are equal, ignoring the case.
  • It returns false if the strings are not equal.

Exceptions in equalsIgnoreCase()

  • When string2 is null, it simply returns false without throwing any exception.
  • However, if string1 is null, it throws a NullPointerException.

Detailed Explanation of Java equalsIgnoreCase()

The Java equalsIgnoreCase() method checks if two strings are equal (contain the same sequence of characters) while ignoring the case differences between them.

For comparing the characters, it follows these rules:

  • If a character is an uppercase letter (A-Z), it is considered equal to its corresponding lowercase letter (a-z).
  • Any other characters that don’t have cases (digits, symbols) are compared as is.

Examples of equalsIgnoreCase()

Example 1: Comparing string literals

public class StringComparisonExample {
    public static void main(String[] args) {
        String str1 = "Test";
        String str2 = "test";

        // Comparing str1 and str2 while ignoring case
        boolean result = str1.equalsIgnoreCase(str2);

        System.out.println("Are str1 and str2 equal (case-insensitive)? " + result);
    }
}

Output:
Are str1 and str2 equal (case-insensitive)? true

Example 2: Comparing different strings:

public class StringComparisonExample {
    public static void main(String[] args) {
        String str1 = "Foo";
        String str2 = "Bar";

        // Comparing str1 and str2 while ignoring case
        boolean result = str1.equalsIgnoreCase(str2);

        System.out.println("Are str1 and str2 equal (case-insensitive)? " + result);
    }
}

Output:
Are str1 and str2 equal (case-insensitive)? false

Example 3: Comparing strings with special characters

public class StringComparisonExample {
    public static void main(String[] args) {
        String str1 = "Hello@123";
        String str2 = "hello@123";

        // Comparing str1 and str2 while ignoring case
        boolean result = str1.equalsIgnoreCase(str2);

        System.out.println("Are str1 and str2 equal (case-insensitive)? " + result);
    }
}

Output:
Are str1 and str2 equal (case-insensitive)? true

Example 4: Comparing strings with null value

public class StringComparisonExample {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = null;

        // Comparing str1 and str2 while ignoring case
        boolean result = str1.equalsIgnoreCase(str2);

        System.out.println("Are str1 and str2 equal (case-insensitive)? " + result);
    }
}

Output:
Are str1 and str2 equal (case-insensitive)? false

Conclusion

In summary, equalsIgnoreCase() is a convenient method in Java’s String class that compares two strings while ignoring case differences. After converting to a common case, it checks if the character sequences are equal. The method returns a boolean value – true if the strings are equal, false if not. It handles null values without throwing exceptions. equalsIgnoreCase() is implemented using regionMatches() for case-insensitive comparisons under the hood. This method is beneficial for case-insensitive string comparisons like usernames and passwords. By understanding its syntax, return type, parameters and examples, developers can utilize equalsIgnoreCase() for their case-insensitive string equality needs in Java.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *