String equalsIgnoreCase() Java Example
In this post, we feature a comprehensive article about the equalsignorecase Java ‘s String Method.
1. Introduction
Java String class has provided the java equalsignorecase method to compare this String
to another String
by ignoring case since version 1. Here is the method’s signature:
boolean equalsIgnoreCase(String anotherString)
The String
class inherits the equals
method from the Object
class. It returns true
if and only if the argument is not null
and is a String
object that represents the same sequence of characters as this object.
public boolean equals(Object anObject)
In this example, I will demonstrate:
- Compare two strings with
equals
andequalsIgnoreCase
methods - Compare two strings with customized methods
- Show the difference when comparing
String
literal andString
object - Show the difference when comparing with the
==
operator andequals(), equalsIgnoreCase()
methods
2. Technologies Used
The example code in this example was built and run with:
- Java 11
- Maven 3.3
- Eclipse
- Junit 4.12
3. Maven Project
3.1 Dependencies
I will include Junit
in the pom.xml
.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.jcg.zheng.demo</groupId> <artifactId>string-equals-unittest</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
3.2 Customized equalsIgnoreCase
In this step, I will create a StringEqualsTest
class which compares two strings with the equals
method after converting them into upper case and lower case.
StringEqualsUtil.java
package org.jcg.zheng; public class StringEqualsUtil { public boolean equalsIgnoreCase_via_uppercase(String data, String compareString) { if( data == null) { if ( compareString == null) { return true; } return false; } else { if ( compareString == null) { return false; } return data.toUpperCase().equals(compareString.toUpperCase()); } } public boolean equalsIgnoreCase_via_lowercase(String data, String compareString) { if( data == null) { if ( compareString == null) { return true; } return false; } else { if ( compareString == null) { return false; } return data.toLowerCase().equals(compareString.toLowerCase()); } } }
4. JUnit Test
4.1 StringLiteralTest
In this step, I will create a Junit test class with three tests:
- The
"=="
operator compares two objects based on the object reference. Please see line 17 and 28. - The
equalsIgnorecase
compares twoString
base on the value. Please see line 19 and 30. - The
equals
compares thisString
object has same value with another object. Please see line 18 and 29.
StringLiteralTest.java
package org.jcg.zheng; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; public class StringLiteralTest { @Test public void equalsOperator_and_equalsMthods_on_object_string() { // == operator compares two objects have the same object reference // equals and equals compare two object has the same content value String firstString = new String("Test"); String secondString = new String("Test"); assertFalse(firstString == secondString); assertTrue(firstString.equals(secondString)); assertTrue(firstString.equalsIgnoreCase(secondString)); } @Test public void equalsOperator_and_equalsMthods_on_object_literal_string() { // Java internally provides String pool for the literal String, String firstString = "Test"; String secondString = "Test"; assertTrue(firstString == secondString); assertTrue(firstString.equals(secondString)); assertTrue(firstString.equalsIgnoreCase(secondString)); } @Test public void test_null() { assertTrue(null == null); String testString = "Demo"; assertFalse(testString.equals(null)); assertFalse(testString.equalsIgnoreCase(null)); } }
Execute mvn clean test -Dtest=StringLiteralTest
and capture the output here.
Output
------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.jcg.zheng.StringLiteralTest Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.108 sec Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
4.2 StringEqualsTest
In this step, I will create a StringEqualsTest
class which will test both equals
and equalsIgnoreCase
from the String
class. It will also test the two customized equalsIgnoreCase
built at step 3.2. It includes six tests and displays the results in the system console.
StringEqualsTest.java
package org.jcg.zheng; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; public class StringEqualsTest { private StringEqualsUtil strUtil = new StringEqualsUtil(); private void compareString(String compareMethod) { String testString = "ABC"; char[] firstChar = { 'A', 'a' }; char[] secondChar = { 'B', 'b' }; char[] thirdChar = { 'C', 'c' }; for (char first : firstChar) { for (char second : secondChar) { for (char third : thirdChar) { char[] matchChars = { first, second, third }; String matchString = String.valueOf(matchChars); switch (compareMethod) { case "equalsIgnoreCase": assertTrue(testString.equalsIgnoreCase(matchString)); System.out.printf("'%s' equalsIgnoreCase '%s', ret = %b\n", testString, matchString, testString.equalsIgnoreCase(matchString)); break; case "test_equals_string": System.out.printf("'%s' equals '%s', ret = %b\n", testString, matchString, testString.equals(matchString)); break; case "test_equals_charArray": System.out.printf("'%s' equals '%s', ret = %b\n", testString, matchChars, testString.equals(matchChars)); break; case "equalsIgnoreCase_via_uppercase": assertTrue(strUtil.equalsIgnoreCase_via_uppercase(testString, matchString)); System.out.printf("'%s' equalsIgnoreCase_via_uppercase '%s', ret = %b\n", testString, matchString, strUtil.equalsIgnoreCase_via_uppercase(testString, matchString)); break; case "equalsIgnoreCase_via_lowercase": assertTrue(strUtil.equalsIgnoreCase_via_lowercase(testString, matchString)); System.out.printf("'%s' equalsIgnoreCase_via_lowercase '%s', ret = %b\n", testString, matchString, strUtil.equalsIgnoreCase_via_lowercase(testString, matchString)); break; } } } } } @Test public void test_equals_charArray() { compareString("test_equals_charArray"); } @Test public void test_equals_string() { compareString("test_equals_string"); } @Test public void test_equalsIgnoreCase() { compareString("equalsIgnoreCase"); } @Test public void test_equalsIgnoreCase_via_lowercase() { compareString("equalsIgnoreCase_via_lowercase"); } @Test public void test_equalsIgnoreCase_via_uppercase() { compareString("equalsIgnoreCase_via_uppercase"); } @Test public void test_null() { assertTrue(strUtil.equalsIgnoreCase_via_uppercase(null, null)); assertFalse(strUtil.equalsIgnoreCase_via_uppercase(null, "NA")); assertFalse(strUtil.equalsIgnoreCase_via_uppercase("NA", null)); assertTrue(strUtil.equalsIgnoreCase_via_lowercase(null, null)); assertFalse(strUtil.equalsIgnoreCase_via_lowercase(null, "NA")); assertFalse(strUtil.equalsIgnoreCase_via_lowercase("NA", null)); } }
Execute mvn clean test -Dtest=StringLiteralTest
and capture the output here.
Output
------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.jcg.zheng.StringEqualsTest 'ABC' equals 'ABC', ret = true 'ABC' equals 'ABc', ret = false 'ABC' equals 'AbC', ret = false 'ABC' equals 'Abc', ret = false 'ABC' equals 'aBC', ret = false 'ABC' equals 'aBc', ret = false 'ABC' equals 'abC', ret = false 'ABC' equals 'abc', ret = false 'ABC' equalsIgnoreCase 'ABC', ret = true 'ABC' equalsIgnoreCase 'ABc', ret = true 'ABC' equalsIgnoreCase 'AbC', ret = true 'ABC' equalsIgnoreCase 'Abc', ret = true 'ABC' equalsIgnoreCase 'aBC', ret = true 'ABC' equalsIgnoreCase 'aBc', ret = true 'ABC' equalsIgnoreCase 'abC', ret = true 'ABC' equalsIgnoreCase 'abc', ret = true 'ABC' equals '[C@a38d7a3', ret = false 'ABC' equals '[C@77f99a05', ret = false 'ABC' equals '[C@63440df3', ret = false 'ABC' equals '[C@3aeaafa6', ret = false 'ABC' equals '[C@76a3e297', ret = false 'ABC' equals '[C@4d3167f4', ret = false 'ABC' equals '[C@ed9d034', ret = false 'ABC' equals '[C@6121c9d6', ret = false 'ABC' equalsIgnoreCase_via_lowercase 'ABC', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'ABc', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'AbC', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'Abc', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'aBC', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'aBc', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'abC', ret = true 'ABC' equalsIgnoreCase_via_lowercase 'abc', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'ABC', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'ABc', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'AbC', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'Abc', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'aBC', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'aBc', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'abC', ret = true 'ABC' equalsIgnoreCase_via_uppercase 'abc', ret = true Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.214 sec Results : Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
Note: As you see at results here, equalsIgnoreCase
comparing string value without considering the case.
5. Summary
In this example, I demonstrated how to use the String equalsIgnoreCase
method and compare it to equals
. I also demonstrated the difference when comparing with "==
” operator and String
literal.
6. Download the Source Code
This was an example on Java String.equalsIgnoreCase()
Method .This example consists of a Maven project which defines seven test classes to demonstrate the equalsIgnoreCase
method’s usages.
You can download the full source code of this example here: String equalsIgnoreCase() Java Example