Computer Science, asked by arumugam41973, 5 months ago

Differentiate between equals() and equalsIgnoreCase()​

Answers

Answered by charanayodhya
1

Answer:

Use equals() in Java to check for equality between two strings.

Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.

Let’s say the following are our two strings −

String one = "qwerty";

String two = "Qwerty";

Both are equal, but the case is different. Since the method ignores case, both of these strings would be considered equal using equalsIgnoreCase() method.

Here, we are checking the same −

if(one.equalsIgnoreCase(two)) {

   System.out.println("String one is equal to two (ignoring the case) i.e. one==two");

}else{

   System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two");

}

However, under equals() case, they won’t be considered equal −

if(one.equals(two)) {

   System.out.println("String one is equal to two i.e. one==two");

}else{

   System.out.println("String one is not equal to String two i.e. one!=two");

}

The following is the final example.

Example

Live Demo

public class Demo {

   public static void main(String[] args) {

      String one = "qwerty";

      String two = "Qwerty";

      if(one.equalsIgnoreCase(two)) {

         System.out.println("String one is equal to two (ignoring the case) i.e. one==two");

      }else{

         System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two");

      }

      if(one.equals(two)) {

         System.out.println("String one is equal to two i.e. one==two");

      }else{

         System.out.println("String one is not equal to String two i.e. one!=two");

      }

   }

}

Output

String one is equal to two (ignoring the case) i.e. one==two

String one is not equal to String two i.e. one!=two

hpoe it help mark as brainleast

Similar questions