Summary
Add a new instance method to java.lang.String
that returns true
if the string is empty or contains only white space, where white space
is defined as any codepoint that returns true when passed to
Character#isWhitespace(int).
Problem
A traditional solution requires the construction of a new string by stripping the instance string of leading or trailing white space.
Ex 1.
// true iff is empty or only contains white space
boolean blank = string.trim().isEmpty();
In Java 8, the in place solution is awkward in its complexity.
Ex. 2
// true iff is empty or only contains white space
boolean blank = string.codePoints().allMatch(Character::isWhitespace);
Solution
The introduction of a new method that avoids any object construction and reduces use complexity.
Ex 3.
// true iff is empty or only contains white space
boolean blank = string.isBlank();
Specification
/**
* Returns {@code true} if the string is empty or contains only
* {@link Character#isWhitespace(int) white space} codepoints,
* otherwise {@code false}.
*
* @return {@code true} if the string is empty or contains only
* {@link Character#isWhitespace(int) white space} codepoints,
* otherwise {@code false}
*
* @see Character#isWhitespace(int)
*
* @since 11
*/
public boolean isBlank() {
- csr of
-
JDK-8200436 String::isBlank
-
- Resolved
-