StringUtils.isBlank() vs String.isEmpty()
StringUtils.isBlank()
checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.
From the linked documentation:
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
For comparison StringUtils.isEmpty:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true
for null
.
java.lang.String.isBlank()
(since Java 11)
java.lang.String.isEmpty()
Difference between isEmpty() and isBlank() Method in java 11
isEmpty()
The java string isEmpty()
method checks if this string is empty. It returns true, if the length of the string is 0 otherwise false e.g.
System.out.println("".isEmpty()); // Prints - True
System.out.println(" ".isEmpty()); //Prints - False
Java 11 - isBlank()
The new instance method java.lang.String.isBlank()
returns true if the string is empty or contains only white space,
where whitespace is defined as any codepoint that returns true when passed to Character#isWhitespace(int).
boolean blank = string.isBlank();
Before Java 11
boolean blank = string.trim().isEmpty();
After Java 11
boolean blank = string.isBlank();
Any scenario in which String.isEmpty() returns true and String.isBlank() returns false for the same input?
The docs say for isEmpty()
:
Returns true if, and only if, length() is 0.
The docs of isBlank()
:
Returns true if the string is empty or contains only white space codepoints, otherwise false.
Emphasis mine.
So isBlank()
seems to cover also all cases of isEmpty()
.
There exists only one string where isEmpty()
returns true
, and that is an empty string ""
. isBlank()
returns true
if an empty string is provided as parameter.
So: no. No case exists where isEmpty()
returns true and isBlank()
returns false.
And if it does, it is a software bug, provided that the documentation describes the intended behaviour. Or else you have to ask Schrödinger.
What is different between isEmpty and isBlank in kotlin
item.isEmpty()
checks only the length of the the string
item.isBlank()
checks the length and that all the chars are whitespaces
That means that
" ".isEmpty()
should returns false" ".isBlank()
should returns true
From the doc of isBlank
Returns true if this string is empty or consists solely of whitespace
characters.
why my StringUtils class doesn't have isBlank()
The library you want to use is the org.apache.commons.lang.StringUtils
and you have to add it to your classpath manually or add it as a dependency to your gradle configuration or any other build tool you are using.
IntelliJ does not know the library out of the box as it is not part of the JDK.
Best way to verify string is empty or null
Haven't seen any fully-native solutions, so here's one:
return str == null || str.chars().allMatch(Character::isWhitespace);
Basically, use the native Character.isWhitespace() function. From there, you can achieve different levels of optimization, depending on how much it matters (I can assure you that in 99.99999% of use cases, no further optimization is necessary):
return str == null || str.length() == 0 || str.chars().allMatch(Character::isWhitespace);
Or, to be really optimal (but hecka ugly):
int len;
if (str == null || (len = str.length()) == 0) return true;
for (int i = 0; i < len; i++) {
if (!Character.isWhitespace(str.charAt(i))) return false;
}
return true;
One thing I like to do:
Optional<String> notBlank(String s) {
return s == null || s.chars().allMatch(Character::isWhitepace))
? Optional.empty()
: Optional.of(s);
}
...
notBlank(myStr).orElse("some default")
Replacement for Java 11 method String.isBlank() in Java 8
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>
- uses a commonly used library
- proven
- uses centralized code
- is very similar to the source that has to be converted
- Not falling for 'Not invented here' syndrome
Related Topics
Difference Between Oracle Jdbc Driver Classes
How to Stop Selenium from Creating Temporary Firefox Profiles Using Web Driver
Where Is Java Installed on MAC Os X
What Does "Atomic" Mean in Programming
How to Get a Spring Bean in a Servlet Filter
Array Initialization Differences Java
Can Anyone Recommend a Simple Java Web-App Framework
When to Catch the Exception VS When to Throw the Exceptions
How to Create a .Jar File or Export Jar in Intellij Idea (Like Eclipse Java Archive Export)
Maven: How to Change Path to Target Directory from Command Line
Why Can This Generic Method with a Bound Return Any Type
Compare Two Objects in Java with Possible Null Values
How to Calculate the Difference Between Two Arraylists
Resultset.Getstring(1) Throws Java.Sql.Sqlexception: Invalid Operation at Current Cursor Position