4

Possible Duplicate:
Why is there no String.Empty in java?

Is there a JDK constant for an empty String, something like String.EMPTY, equaling ""? I can't seem to find it. I would prefer using that rather than "".

Thanks

Community
  • 1
  • 1
amphibient
  • 29,770
  • 54
  • 146
  • 240

5 Answers5

9

No, there isn't. But since the entire world (well, almost) uses Jakarta Commons-Lang, you can, too. Use StringUtils.EMPTY: http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html#EMPTY

Isaac
  • 16,458
  • 5
  • 57
  • 81
4

What's wrong with ""? It behaves like a constant, it's taken from the string pool and it's really short to write, much shorter than String.EMPTY.

Also, if what you need is to test if a string is empty, just do this:

myString.isEmpty();

UPDATE

If the string can be null, better use either isEmpty() or isNotBlank() from Apache Common's StringUtils class.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    There's only one drawback to using `""`: IDE's won't index it so it'd be impossible for you to look for references for that constant (or any string literal, for that matter). – Isaac Oct 09 '12 at 23:20
  • @Isaac Your IDE has full text search, right? –  Oct 09 '12 at 23:22
  • What if myString is null? isEmpty isn't static either so you can't do String.isEmpty(myString) - at least not in Java 6. – Goyuix Oct 09 '12 at 23:27
  • 1
    @Goyuix the fact that the string could be null was not mentioned in the question. For that requirement, use either `isEmpty()` or `isNotBlank()` from Apache's StringUtils class – Óscar López Oct 09 '12 at 23:30
  • @delnan, full text search isn't semantically identical to code-oriented search. Plus, I was referring to the usage of "obvious" string literals in general, not just the empty string. – Isaac Oct 09 '12 at 23:30
  • 1
    @Isaac It's not equivalent, but close enough in my experience. And it beats trading a clear, obvious string literal for a monstrosity like this. But then again, I'm a Python guy, I'm probably biased :-) –  Oct 09 '12 at 23:32
3

Apache Commons does exactly what you want. It has a constant named StringUtils.EMPTY. If you want to take a look, here is the link to the library: http://commons.apache.org/lang/

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40
2

Others have already pointed out the .isEmpty() function, so I'll go one further. I wanted tell if a string is Null or Empty or Whitespace (ie spaces and/or tabs). So I wrote my own method to do it.

//Check to see if a string is null or empty
public static boolean stringIsNullOrEmpty(String s)
{
    return (s == null) || (s.isEmpty());
}

//Check to see if a string is null or empty or whitespace
public static boolean stringIsNullOrWhiteSpace(String s)
{
    return (s == null) || (s.trim().isEmpty());
}

To use these, you would do something like

String myString = "whatever";

if(stringIsNullOrWhiteSpace(myString))
{
    doSomething();
}

Good luck!

Roy
  • 974
  • 6
  • 11
-1

There is nothing in the default JDK that I am aware of. You may need to define your own constant, or import a third party library that has one.

If your intention is to check for empty string you could try the following.

YOUSTRING.isEmpty();

However note that isEmpty is not Null safe, and not in all versions of the JDK.

To be null safe, use "".equals(YOURSTRING);

However this adds empty string into the HEAP each time you do it. Thus it is best to point to a public static final constant, so that there is only ever one on the heap.

Tinman
  • 786
  • 6
  • 18