What is StringUtils.isNoneEmpty in Java?

Overview

isNoneEmpty() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils that accepts a list of strings and checks if any of the strings in the list are empty. The method returns false if at least one of the strings in the given list of strings is considered empty.

How to check if a string is empty

If a string satisfies any of the criteria below, it’s considered to be empty:

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

  2. The string points to a null reference.

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 isNoneEmpty(final CharSequence... css)

Parameters

final CharSequence... cs: the list of character sequences/strings to check.

Return value

This method returns true if none of the strings in the list are empty. Otherwise, it returns false.

Code

Example 1

  • strings = ["hi", "educative", "indiana\njones\t"]

The method returns true because none of the strings are empty.

Example 2

  • strings = ["hi", "educative", "\n\t"]

The method returns true because none of the strings are empty.

Example 3

  • strings = ["", "educative", "hi"]

The method returns false because one of the strings is empty.

Example 4

  • strings = [null, "educative", "hello"]

The method returns false because one of the strings points to a null reference considered to be an empty string.

import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] strings = {"hi", "educative", "indiana\tjones\n"};
System.out.printf("The output of StringUtils.isNoneEmpty() for the strings - %s is %s",
Arrays.toString(strings),
StringUtils.isNoneEmpty(strings));
System.out.println();
strings = new String[]{"hi", "educative", "\n\t"};
System.out.printf("The output of StringUtils.isNoneEmpty() for the strings - %s is %s",
Arrays.toString(strings),
StringUtils.isNoneEmpty(strings));
System.out.println();
strings = new String[]{"", "educative", "hello"};
System.out.printf("The output of StringUtils.isNoneEmpty() for the strings - %s is %s",
Arrays.toString(strings),
StringUtils.isNoneEmpty(strings));
System.out.println();
strings = new String[]{null, "educative", "hello"};
System.out.printf("The output of StringUtils.isNoneEmpty() for the strings - %s is %s",
Arrays.toString(strings),
StringUtils.isNoneEmpty(strings));
System.out.println();
}
}

Output

The output of the code will be as follows:


The output of StringUtils.isNoneEmpty() for the strings - [hi, educative, indiana	jones
] is true
The output of StringUtils.isNoneEmpty() for the strings - [hi, educative, 
	] is true
The output of StringUtils.isNoneEmpty() for the strings - [, educative, hello] is false
The output of StringUtils.isNoneEmpty() for the strings - [null, educative, hello] is false

Free Resources