Introduction
When we deal with StringUtils class in java, we come across various methods. Such as "Trim", "Split", "Remove", "Replace", "IsEmpty", etc. Here in this article, we will learn about the "IsEmpty" method of StringUtils class in java. Cool! Are you wondering about StringUtils IsEmpty and how to use it?
Fine! So let us learn about StringUtils IsEmpty.
Recommended topic - Iteration Statements in Java, and Duck Number in Java.
What is StringUtils.isEmpty in Java?
StringUtils.isEmpty() is a utility method in the Apache Commons Java library that determines whether a provided String is null or empty. (has zero length). If the String is null or has no length, it returns true; otherwise, it returns false.
Syntax - public static boolean isEmpty(CharSequence XYZ).
In the syntax, the parameter is "XYZ", the CharSequence to check. If the CharSequence is empty or null, it will return true; otherwise, it will return false.
Note - Lang version 3.0 changed signature from isEmpty(String) to isEmpty(CharSequence).
How to Import StringUtils
We need the "StringUtils" class from "Apache Commons Lang3" before we move on and use the "IsEmpty" method. It provides utility methods for manipulating and checking strings.
First, you need to import the jar file(commons-lang3-3.12.0.jar) into our project. You can import through a dependency management tool like "Gradle" or "Maven".
Gradle
Add the following line in the "build.gradle" file.
build.gradle
compile 'org.apache.commons:commons-lang3:3.12.0'
Note - The "build.gradle" is Gradle's default build script name.
Maven
Add the following line in the "pom.xml" file.
pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Note
The "pom.xml" is Maven's default build script name.
Now, write the following line to import the StringUtils class.
import org.apache.commons.lang3.StringUtils;
Example
Statement |
Result |
---|---|
StringUtils.isEmpty(null) | true |
StringUtils.isEmpty("") | true |
StringUtils.isEmpty(" ") | false |
StringUtils.isEmpty("characters") | false |
StringUtils.isEmpty(" characters ") | false |
Related Article Apache Server and Hashcode Method in Java.
StringUtils isEmpty() Example in Java
import org.apache.commons.lang3.StringUtils;
/*Make sure to add Apache Commons Lang jar files before importing StringUtils*/
public class StringUtilsIsEmpty {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";
String str3 = null;
String str4 = "something";
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isEmpty(str2));
System.out.println(StringUtils.isEmpty(str3));
System.out.println(StringUtils.isEmpty(str4));
}
}
Output
true
false
true
false
Explanation
- str1 = "" - The length of the string is zero, so the method returns true.
- str2 = " " - The string is not null with a non-zero string length, so the method returns true.
- str3 = null - The string points to a null reference, so the method returns true.
- str4 = "something" - The string is not null, and the length of the string is greater than zero, so the method returns true.
Remember - The isEmpty() method does not check for whitespace. It returns false if the string is whitespace.
We hope you understand StringUtils IsEmpty(), and you can do this code by yourself on Online Java Compiler.
Must Read Conditional Statements in Java