Java Strings for you!

Alaya Hamza
5 min readMar 12, 2021

How to Compare Strings in Java ?!

String is a series of characters. It is a data type that is frequently used in Java, so string comparison is one of the most used Java operations. However, very often developers can’t cope with this operation. If you are trying to solve this problem, you are in the right place! In this article we will present the different ways of string comparison in Java.

1. String comparison with the String class

This method provides 5 different ways to compare strings in Java. We will consider each one.

1.1. Using “==” Comparison Operator

Warning: The “==” operator only compares references, not values. So this is an incorrect way to compare text values. Let’s see an example:

Exemple:

String string1 = "using comparison operator";
String string2 = "using comparison operator";
String string3 = new String("using comparison operator");

assertThat(string1 == string2).isTrue();
assertThat(string1 == string3).isFalse();

As you can see, both variables point to the same String literal, that’s why the first statement is true, but the second is false because string1 is created with a literal and string3 is created with the new operator, which means that they refer to different objects.

1.2 Using equals()

This method compares two strings based on their content. This is a per-character comparison, which ignores the address. If the two strings have the same length and the characters are in the same order, this method considers them equal and returns false. It returns false if the characters do not match.

The strings class proposes two methods

public boolean equals(Object another) compares this string with the specified object.
public boolean equalsIgnoreCase(String another) compares this string with another string, ignoring the case.

Syntaxe:

str1.equals(str2);

Now let’s see some examples:

Exemple:

String string1 = "comparing strings";
String string2 = "comparing strings";

String string3 = "comparing STRINGS";
String string4 = new String("comparing strings");

assertThat(string1.equals(string2)).isTrue();
assertThat(string1.equals(string4)).isTrue();

assertThat(string1.equals(null)).isFalse();
assertThat(string1.equals(string3)).isFalse();

In this example, the variables string1, string2, and string4 are equal because they have the same case and the same value regardless of their address.

For string3, the method returns false, because it is case sensitive.

Also, if one of the two strings is null, the method returns false.

Now let’s look at another example to make sure you understand.

Exemple

public class CompareStrings {    public static void main(String[] args) {        String style = new String("Bold");
String style2 = new String("Bold");
if(style.equals(style2))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}

Here the result will be “Equal”.

1.3. Using equalsIgnoreCase()

As mentioned above, there is also another method, which returns a boolean value. This method ignores the case of characters when comparing strings.

Syntaxe:

str2.equalsIgnoreCase(str1);

Exemple

String string1 = "using equals ignore case";
String string2 = "USING EQUALS IGNORE CASE";

assertThat(string1.equalsIgnoreCase(string2)).isTrue();

1.4. Using compareTo()

This method compares the characters of two strings lexicographically according to a dictionary or a natural order. It returns an integer value that describes whether the first string is less than, equal to or greater than the second string.

Let’s say string1 and string2 are two variables. There are three possible scenarios:

  • string1 == string2 :0
  • string1 > string2 :positive value
  • string1 < string2 :negative value

Syntaxe:

int str1.compareTo(String str2)

It is high time that we take a look at an example:

Exemple:

String author = "author";
String book = "book";
String duplicateBook = "book";

assertThat(author.compareTo(book))
.isEqualTo(-1);
assertThat(book.compareTo(author))
.isEqualTo(1);
assertThat(duplicateBook.compareTo(book))
.isEqualTo(0);

1.5. Using compareToIgnoreCase()

This is the same method as the previous one. The only difference is that this method is case insensitive.

Exemple:

String author = "Author";
String book = "book";
String duplicateBook = "BOOK";

assertThat(author.compareToIgnoreCase(book))
.isEqualTo(-1);
assertThat(book.compareToIgnoreCase(author))
.isEqualTo(1);
assertThat(duplicateBook.compareToIgnoreCase(book))
.isEqualTo(0);

2. Comparison of strings with the Objects class

An Objects utility class contains an equals() method. It can also be useful to compare two strings.

This method first compares the two strings according to their address, and if they are the same, it returns true. If both arguments are null, it returns true, but if one is null, it returns false. This is a case sensitive method because it calls the equals() method of the String class independently.

Let’s try an example.

Exemple:

String string1 = "using objects equals";
String string2 = "using objects equals";
String string3 = new String("using objects equals");

assertThat(Objects.equals(string1, string2)).isTrue();
assertThat(Objects.equals(string1, string3)).isTrue();

assertThat(Objects.equals(null, null)).isTrue();
assertThat(Objects.equals(null, string1)).isFalse();

3. String comparison with Apache Commons

The Apache Commons library contains a utility class for string-related operations, which is called String utils and contains useful methods for string comparison. Let’s have a look at them!

3.1. Using equals() and equalsIgnoreCase()

We have already introduced the equals() method of the String class and we discovered that it does not deal with null values. On the contrary, the equals() method of the StringUtils class also accepts null values. So, we can say that this is the updated version of the one belonging to the String class.

Exemple:

assertThat(StringUtils.equals(null, null))
.isTrue();
assertThat(StringUtils.equals(null, "equals method"))
.isFalse();
assertThat(StringUtils.equals("equals method", "equals method"))
.isTrue();
assertThat(StringUtils.equals("equals method", "EQUALS METHOD"))
.isFalse();

The equalsIgnoreCase() method of StringUtils returns a boolean value. It works the same as equals(), but it ignores the case of string characters.

Exemple:

assertThat(StringUtils.equalsIgnoreCase("equals method", "equals method"))
.isTrue();
assertThat(StringUtils.equalsIgnoreCase("equals method", "EQUALS METHOD"))
.isTrue();

3.2. Using equalsAny() and equalsAnyIgnoreCase()

The first argument of the equalsAny() method is a class and the second is a multi-args type CharSequence. This method will return true if one of the other given strings will match the case of the first string significantly. Otherwise, it will return false.

Now let’s see the following example.

Exemple:

assertThat(StringUtils.equalsAny(null, null, null))
.isTrue();
assertThat(StringUtils.equalsAny("equals any", "equals any", "any"))
.isTrue();
assertThat(StringUtils.equalsAny("equals any", null, "equals any"))
.isTrue();
assertThat(StringUtils.equalsAny(null, "equals", "any"))
.isFalse();
assertThat(StringUtils.equalsAny("equals any", "EQUALS ANY", "ANY"))
.isFalse();

The equalsAnyIgnoreCase() method is the same as the previous one, but in addition this method ignores the case.

Exemple:

assertThat(StringUtils.equalsAnyIgnoreCase("ignore case", "IGNORE CASE", "any")).isTrue();

3.3. Using compare() and compareIgnoreCase()

You already know the compareTo() method of the String class. The compare() method of the StringUtils class is the null-safe version of the previous method, as it deals with null values by considering them as less than a non-null value. Two null values are considered equal.

This method is used to sort a list of strings with null entries.

Exemple:

assertThat(StringUtils.compare(null, null))
.isEqualTo(0);
assertThat(StringUtils.compare(null, "abc"))
.isEqualTo(-1);
assertThat(StringUtils.compare("abc", "bbc"))
.isEqualTo(-1);
assertThat(StringUtils.compare("bbc", "abc"))
.isEqualTo(1);

The compareIgnoreCase() method is the same, but it ignores the case too.

Exemple:

assertThat(StringUtils.compareIgnoreCase("Abc", "bbc"))
.isEqualTo(-1);
assertThat(StringUtils.compareIgnoreCase("bbc", "ABC"))
.isEqualTo(1);
assertThat(StringUtils.compareIgnoreCase("abc", "ABC"))
.isEqualTo(0);

These are the different ways to compare strings in Java. We hope you found it useful.

--

--