Of course! In Java, there are a few ways to check if a string is "not blank," which typically means it is not null, not empty (""), and does not contain only whitespace characters (like spaces, tabs, etc.).
Here are the best ways to do it, from the most modern approach to older, classic methods.
1. The Modern Way: !String.isBlank() (Java 11+)
This is the recommended approach if you are using Java 11 or newer. The isBlank() method was specifically designed for this purpose.
Important: The isBlank() method itself will throw a NullPointerException if the string variable is null. Therefore, you must always include a null check first.
public class StringCheck {
public static void main(String[] args) {
// --- Test Cases ---
String nonEmptyString = "Hello, World!";
String whitespaceString = " \t \n"; // Contains only spaces, tabs, and newlines
String emptyString = "";
String nullString = null;
System.out.println("--- Using !String.isBlank() ---");
checkIfNotBlank(nonEmptyString); // Should be NOT blank
checkIfNotBlank(whitespaceString); // Should be BLANK
checkIfNotBlank(emptyString); // Should be BLANK
checkIfNotBlank(nullString); // Should be BLANK
}
/**
* Checks if a string is not null and does not consist of only whitespace.
* This is the recommended modern approach (Java 11+).
*/
public static void checkIfNotBlank(String text) {
// The check: not null AND not blank
if (text != null && !text.isBlank()) {
System.out.println("\"" + text + "\" is NOT blank.");
} else {
System.out.println("\"" + text + "\" is considered BLANK.");
}
}
}
Output of the code:
--- Using !String.isBlank() ---
"Hello, World!" is NOT blank.
"
" is considered BLANK.
"" is considered BLANK.
"null" is considered BLANK.
2. The Classic Way: trim().isEmpty() (Pre-Java 11)
If you are on an older version of Java (like Java 8), this is the standard way to achieve the same result. You trim() the whitespace from the ends and then check if the resulting string is isEmpty().
public class StringCheckLegacy {
public static void main(String[] args) {
// --- Test Cases ---
String nonEmptyString = "Hello, World!";
String whitespaceString = " \t ";
String emptyString = "";
String nullString = null;
System.out.println("--- Using trim().isEmpty() ---");
checkIfNotBlankLegacy(nonEmptyString);
checkIfNotBlankLegacy(whitespaceString);
checkIfNotBlankLegacy(emptyString);
checkIfNotBlankLegacy(nullString);
}
/**
* Checks if a string is not blank using pre-Java 11 methods.
*/
public static void checkIfNotBlankLegacy(String text) {
// The check: not null AND the trimmed version is not empty
if (text != null && !text.trim().isEmpty()) {
System.out.println("\"" + text + "\" is NOT blank.");
} else {
System.out.println("\"" + text + "\" is considered BLANK.");
}
}
}
The output will be identical to the first example.
3. Using an External Library: Apache Commons Lang
If you are working on a project that already includes the popular Apache Commons Lang library, it provides a very convenient, null-safe method.
Method: StringUtils.isNotBlank(String str)
This is often preferred in projects that use this library because it's a single, highly readable method call that handles the null check for you.
First, you would need to add the dependency (e.g., in Maven's pom.xml):
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
Then, the Java code is very clean:
import org.apache.commons.lang3.StringUtils;
public class StringCheckApache {
public static void main(String[] args) {
String myString = " some content ";
// The check is clean and null-safe
if (StringUtils.isNotBlank(myString)) {
System.out.println("The string is not blank.");
} else {
System.out.println("The string is blank.");
}
String nullString = null;
if (StringUtils.isNotBlank(nullString)) {
// This block will not execute
} else {
System.out.println("A null string is correctly identified as blank.");
}
}
}
Summary & Recommendation
| Method | Java Version | Null-Safe? | Recommendation |
|---|
str != null && !str.isBlank() | 11+ | No (requires str != null) | Best for modern Java. It's the standard, built-in way. |
str != null && !str.trim().isEmpty() | All Versions | No (requires str != null) | Best for Java 8 or older. This is the classic, universally understood pattern. |
StringUtils.isNotBlank(str) | Any | Yes (handles null itself) | Excellent choice if your project already uses or can include Apache Commons Lang. |