16

when comparing strings I prefer not to rely on instance methods lest the string on which the method is called happens to be null. In .NET I just use the static String.Compare(string, string, bool) method. does java provide a similar built-in "null-safe" string compare utility or do I have to implement my own?

John Smith
  • 4,416
  • 7
  • 41
  • 56
  • 3
    libraries have that. there is an apache StringUtils I think, and android has TextUtils – njzk2 Feb 07 '13 at 15:12

7 Answers7

12

No, JDK does not contain such utility. However there are several external libraries that do that. For example org.apache.commons.lang.StringUtils provides null-safe equals(String, String) and equalsIgnoreCase(String, String). Guava has similar utilities too.

AlexR
  • 114,158
  • 16
  • 130
  • 208
2

There isn't. Apache Commons' StringUtils.equals() does it this way:

public static boolean equals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}
Xavi López
  • 27,550
  • 11
  • 97
  • 161
2

String does offer a compareTo method, which is anyway not null-safe (i.e. you need to check for null value before actually calling the comapreTo method on the object).

What you can do is write your own Comparator like this

class StringComparator implements Comparator<String> {

    public int compare(String s1, String s2) {
        if (s1 != null) {
            return s1.compareTo(s2);
        } else {
            if (s2 == null) {
                return 0;
            } else {
                return -1;
            }
    } 

To verify the property just do:

Comparator<String> comp = new Comparator<String>();
System.out.println(comp.compare(s1, s2);

I didn't actually test the code, so take it as a general example. The benefit is that, even though you need to check for null value anyway, you can write it once and use it several times.

You can also take a look at a built in Comparator inside the String class.

drekyn
  • 438
  • 2
  • 7
  • Yes, I find myself adding this type of code to every new project. Seems like there ought to be a `Strings.compare(left, right)` to handle the null check – Kirby Nov 18 '14 at 20:06
1

I guess the answer is no, you have probably to check it before

if(myString != null && myString.equals("string")) {
    // do something
}

Here some more examples

As others pointed out Apache Commons has this, and also Spring:

StringUtils.hasLength()

if(StringUtils.hasLength(myString)) {
    // do something
}
Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
1

If one of the strings to compare is a constant than you can use equals as it handles null.

if("string".equals(mystring)) {
    // do something
}

If you have 2 variables then you can write your own static method:

public static boolean stringEquals(String s1, String s2) {
    if (s1 == null) {
        return s2 == null;
    }
    return s1.equals(s2);
}
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Alepac
  • 1,833
  • 13
  • 24
1

There's a similar question here How to simplify a null-safe compareTo() implementation?

which sagely advises to use the Apache Commons ObjectUtils class

result = ObjectUtils.compare(left, right);
Community
  • 1
  • 1
Kirby
  • 15,127
  • 10
  • 89
  • 104
-2

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

There is the compare to which returns an int. There is also equals which returns a boolean. If you have a string instance it should be able to execute. If you pass instance.equals(null) it will return false. if your instance is null then you will get a null pointer.

public class App
{

public static void main( String[] args )
{
    String myString = "test";

    if(myString.equals(null))
    {
        System.out.println("Its NUll");
    }else
    {
        System.out.println("Not Equal");
    }

}

}
Michael Marr
  • 2,101
  • 14
  • 13
  • 1
    -1: The question was about a **static**, null-safe built-in method to compare Strings, not how to compare Strings. – Xavi López Feb 07 '13 at 15:30