Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for Java 11 method String.isBlank() in Java 8 [duplicate]

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?

like image 459
Murilo Góes de Almeida Avatar asked Dec 20 '19 12:12

Murilo Góes de Almeida


People also ask

Which of the following statement is related to the isBlank () method in Java 11?

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.

What is the difference between isEmpty and isBlank in Java?

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.

What is isBlank () in Java?

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 .

What is difference between StringUtils isEmpty and isBlank?

isEmpty(foo) which helps you avoid null pointers, just like isBlank , but doesn't check for whitespace characters.


1 Answers

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>
  1. uses a commonly used library
  2. proven
  3. uses centralized code
  4. is very similar to the source that has to be converted
  5. Not falling for 'Not invented here' syndrome
like image 145
Nicktar Avatar answered Oct 26 '22 07:10

Nicktar