Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is StringUtils.firstNonBlank 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.

firstNonBlank is a static method of the StringUtils class that returns the first element in the given list of elements that is not empty, null, or whitespace only. If all the elements are blank or the list is null or empty, null is returned.


A character is classified as whitespace using Character. isWhitespace.

How to import StringUtils

StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added 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>

Note: 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 <T extends CharSequence> T firstNonBlank(final T... values)

Parameters

  • final T... values: the list of values to test

To understand what ... is, refer to this shot.

Return value

The function returns the first non-blank value.

Code

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String[] strings = {null, "", "educative"};
System.out.println("First Non-Blank for the strings - " + StringUtils.firstNonBlank(strings));
strings = new String[]{null, "", " ", "educative.io", null, " "};
System.out.println("First Non-Blank for the strings - " + StringUtils.firstNonBlank(strings));
}
}

Explanation

  • string list = [null, "", "educative"]

The output of the method for the string list above is educative, i.e., the first string that is not empty, null, or whitespace only.

  • string list = [null, "", " ", "educative.io", null, " "]

The output of the method for the string list above is educative.io, i.e., the first string that is not empty, null, or whitespace only.

Expected output

First Non-Blank for the strings - educative
First Non-Blank for the strings - educative.io

RELATED TAGS

java
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