AppDividend
Latest Code Tutorials

equalsIgnoreCase() In Java Example | Java String equalsIgnoreCase() Method

0

equalsIgnoreCase() In Java Example | Java String equalsIgnoreCase() Method is today’s topic. As in today’s lecture we will learn about equalsIgnoreCase( ) method in java. As you can see from its name also that the name is equalsIgnoreCase and hence there must be something like equality checking and IgnoreCase shows that Case should be ignored means that we have to compare the equality of two things without considering their cases.

equalsIgnoreCase() In Java

Java String equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. The equalsIgnoreCase() method returns true if the argument is not null and it represents an equivalent String ignoring case, else false.

It is like equals() method but it doesn’t check the case like upper or lower. If any character is not matched, then it returns false otherwise it returns true.

The equalsIgnoreCase() method is a primary method which mainly does the work of comparing two different strings without taking into account the case in which it was written.

It is similar to equals method in java, but the only difference is that it does not care about the case in which is written but equals method take care about the case in which they were written.

After comparing if all the characters are matched, then it will return true else, it will return false just like the boolean that happen.

Let’s see how we can declare this method equalsIgnoreCase.

string2.equalsIgnoreCase(string1);

This is the syntax of method equalsIgnoreCase() where we are doing the work of comparing our two strings that are string1 and string2.

Now you want to know what will be the results after executing the statement so I should clarify that also that this method will return true if both of them string1 and string2 are equivalent after ignoring the Case else it will return us false.

Two strings are necessary for this method to work as there is a comparison between these two strings.

#Example

Input: string1 = "App"
       string2 = "apP"
string2.equalsIgnoreCase(string1);

It will give us the output true.

Don’t confuse with equals() and equalsIgnoreCase() as they are both are almost same, but the difference is of the only Case that in equals() method case is also taken in the account whereas in case of equalsIgnoreCase() method case does not matter.

Let’s see this or try to understand through a diagram as it is the best way to understand any of the things.

 

equalsIgnoreCase() In Java

The equalsIgnoreCase( ) method is the method mainly performed on strings to check whether they are identical or not. But equalsIgnoreCase( ) method only exists in java programming language and not in C, C++ etc.

The equalsIgnoreCase( ) method is the method that shows its output in the boolean form that is in true or false rather than any specific output.

For a better understanding of this topic, we have to prefer an example.

public class Equal {
  public static void main(String[] args) {
    String Str1 = "App Dividend";
    String Str2 = "aPP dIVIDEND";
    String Str3 = "Any";
    System.out.println(Str1.equalsIgnoreCase(Str2)); // true
    System.out.println(Str1.equalsIgnoreCase(Str3)); // false
  }
}

See the following output.

 

Java equalsIgnoreCase() Method

#Java String equalsIgnoreCase() Method Example

Let’s see another example. This time we will take a Java ArrayList.

import java.util.ArrayList;  
public class Word {  
    public static void main(String[] args) {  
        String str1 = "Millie Bobby Brown";  
        ArrayList<String> list = new ArrayList<>();  
        list.add("millie bobby brown");   
        list.add("finn");  
        list.add("gaten");  
        list.add("caleb");  
        list.add("noah");  
        for (String str : list) {  
            if (str.equalsIgnoreCase(str1)) {  
                System.out.println("Millie Bobby Brown is present");  
            }  
        }  
    }  
}

In the above example, there we are comparing the string Millie Bobby Brown with all the ArrayList elements, and if it finds the match, then it will go ahead and print the if condition’s statement.

See the following output.

➜  java javac Word.java
➜  java java Word
Millie Bobby Brown is present
➜  java

In this way, we have learned equalsIgnoreCase() In Java Example Tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.