Advertisement



< 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





Advertisement




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 -




< Prev
Next >
< compareTo() method
toString() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement