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.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact