Java String isAnyEmpty() Utility Method

This page contains the source code of Java String isAnyEmpty() utility method - This method checks, if any of the CharSequences are empty, ("") or null.

Java String isAnyEmpty() Utility Method

The following String isAnyEmpty() utility method checks if any of the CharSequences are empty ("") or null:
public static boolean isAnyEmpty(final CharSequence... css) {
     if (css != null && css.length == 0) {
           return false;
     }
     for (final CharSequence cs : css){
        if (isEmpty(cs)) {
             return true;
        }
     }
     return false;
}
Note that isAnyEmpty() internally made a call to isEmpty() method:
public static boolean isEmpty(final CharSequence cs) {
     return cs == null || cs.length() == 0;
}
JUnit test case for isAnyEmpty(final CharSequence... css) method:
@Test
public void isAnyEmptyTest() {
    assertTrue(isAnyEmpty((String) null));   
    assertFalse(isAnyEmpty((String[]) null));
    assertTrue(isAnyEmpty(null, "foo"));
    assertTrue(isAnyEmpty("", "bar"));   
    assertTrue(isAnyEmpty("bob", ""));     
    assertTrue(isAnyEmpty("  bob  ", null)); 
    assertFalse(isAnyEmpty("foo", "bar"));    
    assertFalse(isAnyEmpty(new String[]{}));  
    assertTrue(isAnyEmpty(new String[]{""}));
}

Related Utility Methods




Comments