I have heard the java string class will finally get the isEmpty method in an upcoming release. Lets hope it is in 1.6, because I can't wait much longer, I can't for the life of me see why this was ignored for so long. Why have I had to put this in so many freakin if statements when this could have been fixed by adding a very easy and simple but amazingly easy to read and useful method.
String randWord;
/*
random code or back in forth between many different classes
*/
String someFunction() {
if(randWord!=null && !randWord.equals("")) {
/*do something*/
}
....
}
all of this could just be replaced and make the code so much more readable with the isEmpty function, becoming the following...
if( !randWord.isEmpty() ) {
/*do something*/
}
I want this functionality and I want it now... I guess since java is now opensource I should just go override the String class to add one simple function! So much value and so simple.

Comments (3)
Yeah, it's annoying when the library writers forget something that you find really useful. Don't even get me started on how annoying the .NET String class is. You can subclass the type, but that still sucks, because you can't create your type from literals ("foo" is always easier than new MyStringSubClass("foo")). That's why I think extension methods are such a cool feature - yes, it will be very easy to abuse them, but they do let you add to standard classes in a way subclassing just doesn't allow sometimes. Very useful.
Posted by Ben | November 20, 2006 7:32 AM
Posted on November 20, 2006 07:32
I wouldn't just replace it... You can get an NPE if the string is null and you try to access a method on it.
Posted by Ploszpe | October 16, 2008 8:12 AM
Posted on October 16, 2008 08:12
With regards to Ploszpe's comment:
to avoid the possible NPE, maybe they should have add a static method that gets the string object as a parameter:
if( !String.isEmpty(randWord) ) {
/*do something*/
}
and since they didn't, the resort is to have your own class for stuff like this:
public class StringUtils {
public static boolean isEmpty(String str){
return str == null || str.length() == 0;
}
}
Posted by targumon | May 5, 2009 3:36 AM
Posted on May 5, 2009 03:36