HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 11 / String isBlank() – Check blank or empty string in Java

String isBlank() – Check blank or empty string in Java

Learn to use String.isBlank() method to determine is a given string is blank or empty or contains only white spaces. isBlank() method has been added in Java 11.

To check is given string does not have even blank spaces, use String.isEmpty() method.

1. String isBlank() Method

This method returns true if the given string is empty or contains only white space code points, otherwise false.

It uses Character.isWhitespace(char) method to determine a white space character.

/**
* returns 	- true if the string is empty or contains only white space codepoints
*			- otherwise false
*/

public boolean isBlank()

2. String isBlank() example

Java program to check if given string is blank or not.

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.println( "ABC".isBlank() );			//false

		System.out.println( " ABC ".isBlank() );		//false

		System.out.println( "  ".isBlank() );			//true

		System.out.println( "".isBlank() );				//true
	}
}

Program output.

false
false
true
true

3. isBlank() vs isEmpty()

Both methods are used to check for blank or empty strings in java. 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.

public class Main 
{
	public static void main(String[] args) 
	{
		System.out.println( "ABC".isBlank() );		//false
		System.out.println( "  ".isBlank() );		//true

		System.out.println( "ABC".isEmpty() );		//false
		System.out.println( "  ".isEmpty() );		//false
	}
}

Program output.

false
true
false
false

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 11 Tutorial

  • Java 11 – New features
  • Java 11 – String.isBlank()
  • Java 11 – String.lines()
  • Java 11 – String.repeat()
  • Java 11 – String.strip()
  • Java 11 – Files.readString()
  • Java 11 – Files.writeString()

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces