title: Checking for a not null, not blank String in Java
body: <p>I am trying to check if a Java String is not <code>null</code>, not empty and not whitespace.</p>
  <p>In my mind, this code should have been quite up for the job.</p>
  <pre><code>public static boolean isEmpty(String s) {
      if ((s != null) &amp;&amp; (s.trim().length() &gt; 0))
          return false;
      else
          return true;
  }
  </code></pre>
  <p>As per documentation, <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim%28%29" rel="noreferrer"><code>String.trim()</code></a> should work thus:</p>
  <blockquote>
  <p>Returns a copy of the string, with leading and trailing whitespace omitted.</p>
  <p>If this <code>String</code> object represents an empty character sequence, or the first and last characters of character sequence represented by this <code>String</code> object both have codes greater than <code>'\u0020'</code> (the space character), then a reference to this <code>String</code> object is returned.</p>
  </blockquote>
  <p>However, <code>apache/commons/lang/StringUtils.java</code> does it a little differently.</p>
  <pre><code>public static boolean isBlank(String str) {
      int strLen;
      if (str == null || (strLen = str.length()) == 0) {
          return true;
      }
      for (int i = 0; i &lt; strLen; i++) {
          if ((Character.isWhitespace(str.charAt(i)) == false)) {
              return false;
          }
      }
      return true;
  }
  </code></pre>
  <p>As per documentation, <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isWhitespace%28char%29" rel="noreferrer"><code>Character.isWhitespace()</code></a>:</p>
  <blockquote>
  <p>Determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:</p>
  <ul>
  <li>It is a Unicode space character (<code>SPACE_SEPARATOR</code>, <code>LINE_SEPARATOR</code>, or <code>PARAGRAPH_SEPARATOR</code>) but is not also a non-breaking space (<code>'\u00A0'</code>, <code>'\u2007'</code>, <code>'\u202F'</code>).</li>
  <li>It is <code>'\t'</code>, U+0009 HORIZONTAL TABULATION.</li>
  <li>It is <code>'\n'</code>, U+000A LINE FEED.</li>
  <li>It is <code>'\u000B'</code>, U+000B VERTICAL TABULATION.</li>
  <li>It is <code>'\f'</code>, U+000C FORM FEED.</li>
  <li>It is <code>'\r'</code>, U+000D CARRIAGE RETURN.</li>
  <li>It is <code>'\u001C'</code>, U+001C FILE SEPARATOR.</li>
  <li>It is <code>'\u001D'</code>, U+001D GROUP SEPARATOR.</li>
  <li>It is <code>'\u001E'</code>, U+001E RECORD SEPARATOR.</li>
  <li>It is <code>'\u001F'</code>, U+001F UNIT SEPARATOR.</li>
  </ul>
  </blockquote>
  <p>If I am not mistaken - or might be I am just not reading it correctly - the <code>String.trim()</code>  should take away any of the characters that are being checked by <code>Character.isWhiteSpace()</code>. All of them see to be above <code>'\u0020'</code>.</p>
  <p>In this case, the simpler <code>isEmpty</code> function seems to be covering all the scenarios that the lengthier <code>isBlank</code> is covering.</p>
  <ol>
  <li>Is there a string that will make the <code>isEmpty</code> and <code>isBlank</code> behave differently in a test case?</li>
  <li>Assuming there are none, is there any other consideration because of which I should choose <code>isBlank</code> and not use <code>isEmpty</code>?</li>
  </ol>
  <p>For those interested in actually running a test, here are the methods and unit tests.</p>
  <pre><code>public class StringUtil {
  
      public static boolean isEmpty(String s) {
          if ((s != null) &amp;&amp; (s.trim().length() &gt; 0))
              return false;
          else
              return true;
      }
  
      public static boolean isBlank(String str) {
          int strLen;
          if (str == null || (strLen = str.length()) == 0) {
              return true;
          }
          for (int i = 0; i &lt; strLen; i++) {
              if ((Character.isWhitespace(str.charAt(i)) == false)) {
                  return false;
              }
          }
          return true;
      }
  }
  </code></pre>
  <p>And unit tests</p>
  <pre><code>@Test
  public void test() {
      
      String s = null; 
      assertTrue(StringUtil.isEmpty(s)) ;
      assertTrue(StringUtil.isBlank(s)) ;
      
      s = &quot;&quot;; 
      assertTrue(StringUtil.isEmpty(s)) ;
      assertTrue(StringUtil.isBlank(s)); 
      
      s = &quot; &quot;; 
      assertTrue(StringUtil.isEmpty(s)) ;
      assertTrue(StringUtil.isBlank(s)) ;
      
      s = &quot;   &quot;; 
      assertTrue(StringUtil.isEmpty(s)) ;
      assertTrue(StringUtil.isBlank(s)) ;
      
      s = &quot;   a     &quot;; 
      assertTrue(StringUtil.isEmpty(s)==false) ;    
      assertTrue(StringUtil.isBlank(s)==false) ;       
      
  }
  </code></pre>
  <p>Update: It was a really interesting discussion - and this is why I love Stack Overflow and the folks here. By the way, coming back to the question, we got:</p>
  <ul>
  <li>A program showing which all characters will make the behave differently. The code is at <a href="https://ideone.com/ELY5Wv" rel="noreferrer">https://ideone.com/ELY5Wv</a>. Thanks @Dukeling.</li>
  <li>A performance related reason for choosing the standard <code>isBlank()</code>. Thanks @devconsole.</li>
  <li>A comprehensive explanation by @nhahtdh. Thanks mate.</li>
  </ul>