I have a project developed in Java 11 and have to adapt in another, but using Java 8.
Some part of this Java 11 project uses the String
method isBlank()
, that is not recognized in Java 8. What is the best approach to adapt this method in Java 8 project?
In Java 11, a new method called isBlank() was added in the String class. The isBlank() method will return true in the below cases: If the string is empty. If the string only contains whitespaces.
isBlank() vs isEmpty() The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters. It does not check the string length.
isBlank() is an instance method that returns true if the string is empty or contains only white space codepoints. This method was introduced in Java 11. If the string contains only white spaces, then applying this method will return true .
isEmpty(foo) which helps you avoid null pointers, just like isBlank , but doesn't check for whitespace characters.
Best approach would be to use Apache Commons StringUtils.isBlank(String)
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With