Apache Commons IO provides the StringUtils Class for checking empty string in Java.
Add the Apache Commons Lang in the pom.xml.
... <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0.1</version> </dependency> ...
Try the following Class.
import org.apache.commons.lang3.StringUtils; public class CheckEmptyString { public static void main(String[] args) { String testString = null; if (StringUtils.isEmpty(testString)) { System.out.println("This string is empty."); } } }
Done =)
Every string has isEmpty() method that checks the same. Why do we need to use apache commons? 🙂
LikeLike
Yes, but that isEmpty() method of a string object provided by jdk6 will throw NullPointerException if the string is null.
LikeLike
Yeah, you are right. Thanks in advance!
LikeLike
you are welcome. =)
LikeLike