does java provide a built-in static String.Compare method?

does java provide a built-in static String.Compare method?

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?

Solutions/Answers:

Answer 1:

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.

Answer 2:

There isn’t. Apache CommonsStringUtils.equals() does it this way:

public static boolean equals(String str1, String str2) {
    return str1 == null ? str2 == null : str1.equals(str2);
}

Answer 3:

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.

Answer 4:

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
}

Answer 5:

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

Answer 6:

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

Answer 7:

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

}

}

References