tags:

views:

1041

answers:

6

This surely has been asked before, but Googling doesn't find it. Is there, in any of the standard java libraries (including apache/google/...), a static isNullOrEmpty() method for Strings?

+3  A: 

No, which is why so many other libraries have their own copy :)

Jon Skeet
+12  A: 
  • StringUtils.isEmpty(str) or StringUtils.isNotEmpty(str)
  • StringUtils.isBlank(str) or StringUtils.isNotBlank(str)

from Apache commons-lang.

The difference between empty and blank is : a string consisted of whitespaces only is blank but isn't empty.

I generally prefer using apache-commons if possible, instead of writing my own utility methods, although that is also plausible for simple ones like this.

Bozho
I was confused between the distinction between blank and empty, so I looked it up; blank means, "", null, and any amount of whitespace; empty is just "" and null.
James McMahon
+1  A: 
public static boolean isNull(String str) {
        return str == null ? true : false;
    }

    public static boolean isNullOrBlank(String param) {
        if (isNull(param) || param.trim().length() == 0) {
            return true;
        }
        return false;
    }
Yashwant Chavan
Yashwant, you don't need the return true, return false lines: return str==null || str.trim().length()==0;
Bruno Rothgiesser
`return str == null ? true : false;` - classic no-no. Why not `return str == null`?
Yuval A
Why even implement the first method when you can write if (str == null)?
Adamski
Just a note about the second method, that is not how it is implemented in the Jakarta commons, mainly because when you execute the "trim" method you are creating a new object, and in their implementation they actually traverse the string from the start, and if they find anything else than a space, they will stop the method and return true. It is probably faster and it is likely to be better on memory.It is a tiny little thing but for these reasons it is better to use an external library.
Ravi Wallau
A: 

For new projects, I've started having every class I write extend the same base class where I can put all the utility methods that are annoyingly missing from Java like this one, the equivalent for collections (tired of writing list != null && ! list.isEmpty()), null-safe equals, etc. I still use Apache Commons for the implementation but this saves a small amount of typing and I haven't seen any negative effects.

Brian Deterling
Creating your own "Universal Base Class" is questionable on many counts, and a really limiting design strategy in a language without multiple inheritance.
Don Roby
Java already has a universal base class called Object. I can extend my base class just as easily as that one. Like I said, I haven't seen any problems yet, but I'm interested in hearing about the potential pitfalls - maybe I'll submit an S/O question asking about it.
Brian Deterling
A: 

I've seen this method written a few times in projects I've been on but I have to say I've never written it myself, or called it either ... Generally I find null and empty are completely distinct conditions and I have no reason to ever conflate them.

EJP
A: 

You can add one

public static boolean isNullOrBlank(String param) { 
    return param == null || param.trim().length() == 0;
}

I have

public static boolean isSet(String param) { 
    // doesn't ignore spaces, but does save an object creation.
    return param != null && param.length() != 0; 
}
Peter Lawrey