dcsimg

How do I check if a String is empty?

John Zukowski

Assuming it isn't null, it is best to just check its length.


  String s = ..;
  if (s.length() == 0) {
    System.out.println("I'm empty");
  }
You may want to trim() the contents first to remove leading/trailing whitespace.

Starting with Java 6, you can call the new isEmpty() method of String, but that doesn't trim the string first.