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?

1
  • 3
    libraries have that. there is an apache StringUtils I think, and android has TextUtils Commented Feb 7, 2013 at 15:12

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

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.

1 Comment

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
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
}

Comments

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);

Comments

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);
}

Comments

-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");
    }

}

}

1 Comment

-1: The question was about a static, null-safe built-in method to compare Strings, not how to compare Strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.