Pages

Tuesday, May 28, 2019

Java 11 String API isBlank() Method Example

1. isBlank Method Overview

In this tutorial, We'll learn about new java 11 string isBlank() method and explanation with examples. And also how to check the string is blank or not using isBlank() method.

This method is introduced in Java 11.
Read complete article on What is new in Java 11 String API.

Java 11 String API isBlank() Method Example

Syntax

 

public boolean isBlank()


Basically, isBlank() method returns true if the string is empty or contains only white space codepoints, otherwise false.

String API has another method isEmpty() which also checks the string is empty or not.

2. isBlank Method examples


Let us write a program to check the given string is empty or not.

2.1 isBlank() Example 1

 

Refer the below program. Created input string with contents "java-w3schools" and it is not blank. Now this method should return false.

String input = "java-w3schools";
boolean isBlank = input.isBlank();
System.out.println(isBlank);

Output:

Compile this program and see the output below.

false

2.2 isBlank Example 2

 

Let us take blank "" input string now . This method should return true now.

String input2 = "";
isBlank = input2.isBlank();
System.out.println(isBlank);

Output:

true


2.3 Multiple white spaces example

 

Created another string with 3 white spaces like "   ". Now see what would be output of isBlank() method.

String input3 = "   ";
isBlank = input3.isBlank();
System.out.println(isBlank);

Output:

true

2.4 All isBlank Example at one place


// 1. Empty
String java11Str1 = "";
System.out.println("java11 string 1 is blank :: " + java11Str1.isBlank());

//2. Single white space
String java11Str2 = " ";
System.out.println("java11 string 2 is blank :: " + java11Str2.isBlank());

//3. Multiple white spaces
String java11Str3 = "   ";
System.out.println("java11 string 3 is blank :: " + java11Str3.isBlank());

//4. With value
String java11Str4 = "hello";
System.out.println("java11 string 4 is blank :: " + java11Str4.isBlank());

Output:

java11 string 1 is blank :: true
java11 string 2 is blank :: true
java11 string 3 is blank :: true
java11 string 4 is blank :: false

3. Internal Implementation

Below is the internal code of isBlank() method. Internally it passes each character to Character.isWhitespace(int) method to check it is blank or not.

public boolean isBlank() {
    return indexOfNonWhitespace() == length();
}

private int indexOfNonWhitespace() {
    return isLatin1() ? StringLatin1.indexOfNonWhitespace(value)
                      : StringUTF16.indexOfNonWhitespace(value);
}


4. Conclusion

In this tutorial, We've seen how to use isBlank() method to check the string is blank or not.

And also seen example programs and what happens if input string has only white spaces (can be multiple).

Example code snippets shown in this article is available on GitHub.

No comments:

Post a Comment

Please do not add any spam links in the comments section.