You might want to test for if a String is empty many a times. Before we jump onto the numerous solutions available let us take a look at how we define “Empty String”
The difference in the two methods given by Apache and Java are dependent on how we define an empty string.
- Java String.isEmpty returns a boolean true if the string’s length is zero. If the string has null it throws NullPointerException
- Apache StringUtils.isEmpty returns a boolean true if the string is either null or has length is zero
Thus its purely dependent on how you are defining “empty string” in your program which will decide which function to use…BTW if you want to skip using Apache Commons funciton and would want to stick to java then you can have your own function like this:
public static boolean isEmptyOrNull(String strStringToTest) {
return strStringToTest == null || strStringToTest.trim().isEmpty();
}
Comments