Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
isblank
stringutils
communitycreator

What is StringUtils.isBlank in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

isBlank() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils which is used to check if the given string is blank.

When is a string considered blank?

If a string satisfies any of the criteria below, then the string is considered to be blank:

  1. The length of the string is zero or the string is empty.

  2. The string points to a null reference.

  3. The string contains only whitespace characters.


A character can be classified as a whitespace character using the method Character.isWhitespace().

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

Syntax


public static boolean isBlank(final CharSequence cs)

Parameters

final CharSequence cs: The character sequence/string to check.

Return value

This method returns true if the string is blank. Otherwise, it returns false.

Code

Example 1

  • string - "543234"

The method returns false because the string is not null and not empty.

Example 2

  • string - ""

The method returns true because the length of the string is zero.

Example 3

  • string - null

The method returns true because the string points to a null reference.

Example 4

  • string - " \n\t"

The method returns true because the string contains only whitespace characters.

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String s = "543234";
        System.out.printf("The output of StringUtils.isBlank() for the string - '%s' is %s", s, StringUtils.isBlank(s));
        System.out.println();

        s = "";
        System.out.printf("The output of StringUtils.isBlank() for the string - '%s' is %s", s, StringUtils.isBlank(s));
        System.out.println();

        s = null;
        System.out.printf("The output of StringUtils.isBlank() for the string - '%s' is %s", s, StringUtils.isBlank(s));
        System.out.println();

        s = "    \n\t";
        System.out.printf("The output of StringUtils.isBlank() for the string - '%s' is %s", s, StringUtils.isBlank(s));
        System.out.println();
    }
}

Output

The output of the code will be as follows:


The output of StringUtils.isBlank() for the string - '543234' is false
The output of StringUtils.isBlank() for the string - '' is true
The output of StringUtils.isBlank() for the string - 'null' is true
The output of StringUtils.isBlank() for the string - '    
	' is true

RELATED TAGS

java
isblank
stringutils
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring