< Prev
Next >



String equalsIgnoreCase()




equalsIgnoreCase() method of String class performs case-insensitive equality check on values in two String objects, by ignoring the case of each character in a String.




Signature of equalsIgnoreCase()


public boolean equalsIgnoreCase(String str)
Performs a case-insensitive equality check between the invoked String object value and the value in String str and returns an Boolean -
  • true, if the invoked String object and String str have a same value.
  • false, if the invoked String object and String str have a different value.





equalsIgnoreCase() example -:


We are performing case-insensitive equality check on the values of String objects by calling equalsgnoreCase() method.
// Program to find whether value of String is same as value value of another String(Ignoring cases)


class IgnoreCase
{
public static void main(String[] ar)
{
String str1 = new String("WELCOME");
String str2 = new String("welcome");
String str3 = new String("Welcome");
String str4 = new String("WelComE");

System.out.println("First  String is " + str1);
System.out.println("Second String is " + str2);
System.out.println("Third  String is " + str3);
System.out.println("Fourth String is " + str4);

System.out.println("Values in first and second String equal?  " + str1.equalsIgnoreCase(str2));
System.out.println("Values in first and third  String equal?  " + str1.equalsIgnoreCase(str3));
System.out.println("Values in first and fourth String equal?  " + str1.equalsIgnoreCase(str4));

}
}


Output is :



First  String is WELCOME
Second String is welcome
Third  String is Welcome
Fourth String is WelComE
Values in first and second String  equal?  true
values in first and third  String  equal?  true
Values in first and fourth String  equal?  true


Program Analysis







Difference betweeen equals() and equalsIgnoreCase() method -:


// Displaying the difference between equals() and equalsIgnoreCase() methods.


class IgnoreCase
{
public static void main(String[] ar)
{
String str1 = new String("Good Day");
String str2 = new String("GOOD DAY");

System.out.println("First  String is "  + str1);
System.out.println("Second String is " + str2);

//Case-sensitive check on values of first and second String object
System.out.println("equals() on first and second String object? " + str1.equals(str2));

//Case-insensitive check on values of first and second String object
System.out.println("equalsIgnoreCase() on first and second String object? " + str1.equalsIgnoreCase(str2));
}
}


Output is :


First  String is Good Day
Second String is GOOD DAY
equals() method on first and second String object? false
equalsIgnoreCase() on first and second String object?  true





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< compareTo() method
toString() method >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2