Wednesday 8 February 2017

Java String equelsIgnoreCase() Methods



  Sometimes we do not care if a letter is uppercase or lower. The EqualsIgnoreCase method treats both cases as equal. It does not care about upper and lower.
This method performs String comparison irrespective of the case. The general form is:

boolean equalsIgnoreCase(String anotherString)

For Examples:

s1.equalsIgnoreCase(s2);

Compare two string s1 and s2 irrespective of their case and returns true if they are equals.
Consider the following example to see the difference between the methods equals() and equalsIgnoreCase().

EqualsIgnoreTest.java


 class EqualsIgnoreTest
 {
  public static void main(String arg[])
  {
   String s1="Java";
   String s2="Java";
   String s3="JAVA";

   System.out.println(s1+" equals "+s2+" is: " +s1.equals(s2));
   System.out.println(s1+" equals "+s3+" is: "+s1.equals(s3));

   System.out.println(s1+" equalsIgnoreCase "+s3+" is: "+s1.equalsIgnoreCase(s3));
  }
 }

The output is:

Java equals Java is: true
Java equals JAVA is: false
Java equalsIgnoreCase JAVA is: true


0 comments:

Post a Comment

Powered by Blogger.

Stats