How do I check if a String is empty?
Created May 8, 2012
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.