Java String isEmpty() Utility Method

Java String Utility Methods


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

Java String isEmpty() Utility Method

The following String isEmpty() utility method checks if a CharSequence 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  "));
}

Comments