Java String equalsIgnoreCase example

String's equalsIgnoreCase method is similar to equals method but it ignores case. So if Strings have different cases, then also it will considered as equals.

For example:
String str1="Java2blog.com"
String str2="JAVA2BLOG.COM"

str1.equalsIgnoreCase(str2) will return true.

Method Signature:

public boolean equalsIgnoreCase(Object anObject)

String equalsIgnoreCase Example:

package org.arpit.java2blog;

public class StringEqualsExamplesMain {
 public static void main(String[] args) {
  String str1 = "Hello world from java2blog.com";
  String str2 = new String("Hello world from java2blog.com");
  String str3 = "Hello world from JAVA2BLOG.COM";

  if (str1.equalsIgnoreCase(str2)) {
   System.out.println("1st and 2nd Strings are equal");
  } 
  else {
   System.out.println("1st and 2nd Strings are not equal");
  }

  if (str1.equalsIgnoreCase(str3)) {
   System.out.println("1st and 3rd Strings are equal");
  } 
  else {
   System.out.println("1st and 3rd Strings are not equal");
  }
 }
}

When you run above program, you will get below output:
1st and 2nd Strings are equal
1st and 3rd Strings are equal

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012