Java: String equalsIgnoreCase(Object anObject) method

In this article, we will discuss string comparison using String’s equalsIgnoreCase() method, which ignores case differences while comparing 2 string contents

 

equalsIgnoreCase(Object anObject) method: This String method is used to perform string comparison, ignoring case differences between 2 string contents

Note: Another variation to this method equals(Object anObject) does String comparison considering case differences

 

Method Signature:

public boolean equalsIgnoreCase(Object anObject);

 

Returns:

Returns a boolean value either true or false

  • True –> if both string contents are equal, ignoring CASE differences
  • False –> if both string contents are NOT equal, ignoring CASE differences

 

Examples on equalsIgnoreCase() method:

String comparison program using equalsIgnoreCase() method

StringEqualsIgnoreCaseMethod.java

package in.bench.resources.string.methods;

public class StringEqualsIgnoreCaseMethod {

	public static void main(String[] args) {

		// string literal and objects
		String str1 = "bench";
		String str2 = new String("bench");
		String str3 = new String("BENCH");

		// comparing string literal and string object
		boolean bool1 = str1.equalsIgnoreCase(str2);

		// printing to the console
		System.out.println("str1 & str2 are equal ? : " + bool1);

		// comparing string literal and string object
		boolean bool2 = str1.equalsIgnoreCase(str3);

		// printing to the console
		System.out.println("str2 & str3 are equal ? : " + bool2);

		// in-line equality check
		System.out.println("str1 & ORACLE are equal ? : " 
				+ str1.equalsIgnoreCase("ORACLE"));
	}
}

Output:

str1 & str2 are equal ? : true
str2 & str3 are equal ? : true
str1 & ORACLE are equal ? : false

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: String format(String format, Object… args) method
Java: String equals(Object anObject) method