What is whitespace in Java?
A character is a Java whitespace character if and only if it satisfies one of the following criteria:
a. It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
b. It is '\t', U+0009 HORIZONTAL TABULATION.
c. It is '\n', U+000A LINE FEED.
d. It is '\u000B', U+000B VERTICAL TABULATION.
e. It is '\f', U+000C FORM FEED.
f. It is '\r', U+000D CARRIAGE RETURN.
g. It is '\u001C', U+001C FILE SEPARATOR.
h. It is '\u001D', U+001D GROUP SEPARATOR.
i. It is '\u001E', U+001E RECORD SEPARATOR.
j. It is '\u001F', U+001F UNIT SEPARATOR.
isBlank() method is introduced in Java11. This method return true, if the string is empty or contains only white space codepoints, otherwise false.
Signature
public boolean isBlank()
Example
str.isBlank()
StringContainWhiteSpacesDemo.java
public class StringContainWhiteSpacesDemo {
private static boolean containOnlyWhiteSpaces(String str) {
if (str == null) {
return false;
}
return str.isBlank();
}
public static void main(String args[]) {
String str1 = "Hello";
String str2 = "\n\t\f\r \n\t";
System.out.println("is str1 contains only whitespaces : " + containOnlyWhiteSpaces(str1));
System.out.println("is str2 contains only whitespaces : " + containOnlyWhiteSpaces(str2));
}
}
Output
is str1 contains only whitespaces : false is str2 contains only whitespaces : true
No comments:
Post a Comment