Java String isEmpty() Utility Method - Check String is Empty or Null

This page contains the source code of the Java String isEmpty() utility method - This method checks if a CharSequence or String is empty ("") or null.

Java String isEmpty() Utility Method

The following String isEmpty() utility method checks if a CharSequence or String is empty ("") or null:

public static boolean isEmpty(final CharSequence cs) {
     return cs == null || cs.length() == 0;
}
JUnit test case for the above method:
@Test
public void isEmptyTest() {
    assertTrue(isEmpty(null));
    assertTrue(isEmpty(""));
    assertFalse(isEmpty(" "));
    assertFalse(isEmpty("bob"));
    assertFalse(isEmpty("  bob  "));
}

Related Utility Methods


Comments