Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String equalsIgnoreCase() vs JDK 7 Objects.equals(Object, Object)

Do I lose anything by replacing the following:

    public final boolean testEquals(String left, String right) {
        if ((left == null && right != null) || (left != null && right == null)) {
            return false;
        } else if (left == null && right == null) {
            return true;
        } else {
            return left.equalsIgnoreCase(right);
        }
    }

With this from the JDK 7 Objects class:

    public final boolean testEquals(String left, String right) {
        return Objects.equals(left,right);
    }

If I lose the case insensitivity, is replacing it with

Objects.equals(left.toLowerCase(),right.toLowerCase()); 

enough? Does it make sense to?

like image 780
Janek Nikicicz Avatar asked Aug 05 '15 13:08

Janek Nikicicz


People also ask

What is the difference between equals () and equalsIgnoreCase () methods in Java?

Difference between equals() vs equalsIgnoreCase() in JavaUse equals() in Java to check for equality between two strings. Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.

What is the purpose of using equalsIgnoreCase () in String Program?

Java String equalsIgnoreCase() Method The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

Can you use equals () with objects?

The equals() method is a static method of the Objects class that accepts two objects and checks if the objects are equal. If both the objects point to null , then equals() returns true .

Can Object equals to String?

The equals() method of the String class It is overridden, this method accepts a String value and compares it with the current object and returns true only if the character sequences in the both String objects are exactly same.


2 Answers

If you are not only limited to JDK you can use

StringUtils.equalsIgnoreCase(CharSequence str1, CharSequence str2)

from apache-commons lang which is null-safe and will return

 StringUtils.equalsIgnoreCase(null, null)   = true
 StringUtils.equalsIgnoreCase(null, "abc")  = false
 StringUtils.equalsIgnoreCase("abc", null)  = false
 StringUtils.equalsIgnoreCase("abc", "abc") = true
 StringUtils.equalsIgnoreCase("abc", "ABC") = true
like image 172
Pshemo Avatar answered Oct 21 '22 10:10

Pshemo


The method String.equalsIgnoreCase(String) is "unfortunately" a little more complex. You can look into the code, but it is well explained in the javadoc itself:

Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:

  • The two characters are the same (as compared by the == operator)
  • Applying the method Character.toUpperCase(char) to each character produces the same result
  • Applying the method Character.toLowerCase(char) to each character produces the same result
like image 33
dotvav Avatar answered Oct 21 '22 12:10

dotvav