Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Java equalsIgnoreCase()

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

A wide range of operations can be performed on Strings in Java. Operations like comparison, accessing, replacing, searching and removing are quite common in Strings. Comparison of strings is different from comparing values of other primitive data types as Strings are objects. String Objects need specific functions to carry out relational operations. In Java, there are many ways to compare strings. In this blog, we shall discuss the method to check the equality of two strings by ignoring their case.

equalsIgnoreCase()

As the name suggests, the equaleIgnoreCase () function checks if two strings are equal or not by ignoring their case. This means that irrespective of whether the characters are in upper case or lower case if the letters match, then the function returns true. If any letter in both the strings does not match at all, it returns false.

Syntax: 

str2.equalsIgnoreCase(str1);

 

Parameters:

str1, str2 - the strings to be checked for equality.

Returns type - boolean

Signature

public boolean equalsIgnoreCase(String str)  

 

Where str is the string to be compared with.

Internal Implementation

import java.lang.*;
class Main 
{
  public static void main(String[] args) 
  {
    String s = "HELLO";
    String t = "hELlo";
    int flag=0;
    if (s.length()==t.length())
    {
        for(int i=0;i<s.length();i++)
        {
            if (s.charAt(i)!=t.charAt(i))
            {
                if (s.substring(i,i+1).equals(t.substring(i,i+1).toUpperCase()) || s.substring(i,i+1).equals(t.substring(i,i+1).toLowerCase()))
                flag=1;
                else flag=0;
            }
            else flag=1;
        }
    }
    else flag=0;
    if (flag==0)
    System.out.println("Not Equal");
    else
    System.out.println("Yes Equal");

  }
}

 

Output:

Yes Equal

 

Explanation

The above code sample shows how to check if one string is equal to another after ignoring their case, without using the equalsIgnoreCase() method. First, it checks if the length of the two strings is equal. Next, it checks both the strings character by character. If the characters are the same but in a different case, it converts one of the string’s characters into another case to check the equality. If none of the above conditions is satisfied then the strings are said to be unequal even after ignoring their case.

Use of equalsIgnoreCase()

The entire code shown above can be replaced with the equalsIgnoreCase() method.

class Main 
{
    public static void main(String[] args) 
    {
        String str1 = "HellO WORld";
        String str2 = "hello world";
        String str3 = "HELLO WORLD";
        String str4 = "hello";
        Boolean result;

        // comparing str1 with str2
        result = str1.equalsIgnoreCase(str2);
        System.out.println(result);

        // comparing str1 with str3
        result = str1.equalsIgnoreCase(str3);
        System.out.println(result);

        // comparing str2 with str4
        result = str2.equalsIgnoreCase(str4);
        System.out.println(result);
    }
}

 

Output:

true
true
false

 

Explanation

The code shown above defines four different strings - str1, str2, str3 and str4. Comparing str1, str2 and str3 with the equaleIgnoreCase() method returns true as they read the same word after ignoring the case of the individual letters. The fourth string is entirely different from the other three strings as it lacks a few characters, hence the use of equalsIgnoreCase on str4 and any of the three other strings results in false.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Can ‘==’ be used to compare two strings in Java?

‘==’ can be used to compare two strings, but only for reference comparison. It checks if the two string objects point to the same memory location.

What is the difference between str1.equalsIgnoreCase("abc") and "abc".equalsIgnoreCase(str1), where str1 is a String?

Both the statements will return true if the value of str1 is “abc”. However, if the value of str1 is null, str1.equalsIgnoreCase("abc") will throw a NullPointerException while “abc”.equalsIgnoreCase(str1) will return false.

Conclusion

In this article, we have extensively discussed the equalsIgnoreCase() function in Java. We briefly discussed the use of the equalsIgnoreCase() method and then moved on to see how using this method can help reduce a significant amount of code in a program.

Feeling curious? Coding Ninjas has you covered. Check out our articles on the StringBuffer classStringBuilder class, and StringTokenizers in Java. Follow our Guided path for Programming in Java here.

Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and AlgorithmsMachine LearningDeep LearningCloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! 

Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations.

Upvote our blogs if you find them insightful and engaging! Happy Coding!

Topics covered
1.
Introduction
2.
equalsIgnoreCase()
2.1.
Signature
2.2.
Internal Implementation
2.2.1.
Explanation
2.3.
Use of equalsIgnoreCase()
2.3.1.
Explanation
3.
Frequently Asked Questions
3.1.
Can ‘==’ be used to compare two strings in Java?
3.2.
What is the difference between str1.equalsIgnoreCase("abc") and "abc".equalsIgnoreCase(str1), where str1 is a String?
4.
Conclusion