Shortest solution I can think of:

if (string.trim().length() > 0) ...

This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match() with a regexp such as:

if (string.matches(".*\\w.*")) ...

...which checks for at least one (ASCII) alphanumeric character.

Answer from Carl Smotricz on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › checking for empty or blank strings in java
Checking for Empty or Blank Strings in Java | Baeldung
January 8, 2024 - The most convenient way is to use Apache Commons Lang, which provides helpers such as StringUtils.isBlank. If we want to stick to plain Java, we can use a combination of String#trim with either String#isEmpty or String#length.
🌐
TutorialsPoint
tutorialspoint.com › check-if-a-string-is-whitespace-empty-or-null-in-java
Check if a String is whitespace, empty ("") or null in Java
public class Demo { public static void main(String[] args) { String myStr = ""; if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) { System.out.println("String is not null or not empty or not whitespace"); } else { System.out.println("String is null or empty or whitespace"); } } }
🌐
Medium
medium.com › @AlexanderObregon › javas-string-isblank-method-explained-be7064a144a7
Java’s String.isBlank() Method Explained | Medium
January 10, 2025 - When receiving user input, particularly from forms or text fields, it’s important to handle cases where the input might be empty or consist only of whitespace. Using isBlank() makes such checks more concise and avoids potential errors.
🌐
STechies
stechies.com › java-string-null-empty-whitespace
Check if String is Empty, Null or Whitespace Using Java
In this code snippet, we imported the Apache Commons Lang library, which uses these two StringUtils.isEmpty() and StringUtils.isBlank() static methods to specify if the Java string is not null, not empty, and not white space only. Users can easily employ these methods, and the Apache Commons Lang approach is specifically concise. However, to use this library, your Java IDE must support this library as a standard part of the Java String. It is one of the most recommended methods in Java from Java 7 onwards that allows users to check whether a string is empty, null, or has whitespace.
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-check-if-a-string-in-java-contains-only-whitespaces
Program to check if a String in Java contains only whitespaces - GeeksforGeeks
July 12, 2025 - Examples: Input: str = " " Output: ... the leading whitespaces in the string. Syntax: str.trim() Then we can use the isEmpty() method of String class to check if the resultant string is empty or not....
🌐
Coderanch
coderanch.com › t › 203109 › java › Efficient-Check-Empty-String
Efficient Way to Check Empty String (Performance forum at Coderanch)
We use (string != null || string.trim().length() == 0) to check if string is empty. Calling a method which actually iterate through string and checks for non-whitespace character .
🌐
TutorialsPoint
tutorialspoint.com › Checking-for-Null-or-Empty-or-White-Space-Only-String-in-Java
Checking for Null or Empty or White Space Only String in Java.
We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0. ... import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "tutorialspoint"; // prints length of string ...
Find elsewhere
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › check if a string is null, empty or blank in java
Check if a String is Null, Empty or Blank in Java
June 4, 2023 - Straightforward and concise approach to checking for whitespace in a string. Improved code readability and reduced complexity, especially for empty or whitespace input validation. Usable in various scenarios due to its simplicity. ... Available only from Java 11 onwards, limiting compatibility with older Java versions.
🌐
TutorialsPoint
tutorialspoint.com › program-to-check-if-a-string-in-java-contains-only-whitespaces
Program to check if a String in Java contains only whitespaces
In the following example, we will ... check_whitespace(String str) { // Check whether the string is null or empty if (str == null || str.isEmpty()) { return false; } // The for loop iterate through each character of the string for (int ...
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
Checking for Null or Empty or White Space Only String in Java | InfoWorld
September 2, 2011 - /** * Demonstrate checking for String that is not null, not empty, and not white * space only using standard Java classes. * * @param string String to be checked for not null, not empty, and not white * space only. * @return {@code true} if provided String is not null, is not empty, and * has at least one character that is not considered white space.
🌐
Blogger
marxsoftware.blogspot.com › 2011 › 09 › checking-for-null-or-empty-or-white.html
Inspired by Actual Events: Checking for Null or Empty or White Space Only String in Java
Its Javadoc documentation says as much: 'Checks if a String is not empty (""), not null and not whitespace only.' The above approaches and variations on the above approaches can be used in Java to determine if a String is not null, not empty, and not white space only.
🌐
Vultr Docs
docs.vultr.com › java › examples › check-if-a-string-is-empty-or-null
Java Program to Check if a String is Empty or Null | Vultr Docs
December 17, 2024 - Here, StringUtils.isBlank() checks if str is null, empty, or made solely of whitespace (spaces, tabs, new line characters). Very useful when the mere presence of whitespace should also classify the string as "empty".
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-isempty-method-with-example
Java String isEmpty() method
public class Example{ public static ... whitespace: false System.out.println("str2 is empty or whitespace: " + isEmptyOrWhitespace2); } } The String.isEmpty() method is a simple method to check if a string is empty in Java....
🌐
HowToDoInJava
howtodoinjava.com › home › java 11 › java string isblank() – check blank or empty string
Java String isBlank() - Check Blank or Empty String
December 12, 2022 - The isBlank() method has been added in Java 11. To check if a given string does not have even blank spaces, use String.isEmpty() method. It returns true if the given string is empty or contains only white space code points, otherwise false.
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - If the String is blank, after removing all whitespaces, it'll be empty, so isEmpty() will return true.
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
class Main { public static void main(String[] args) { // create a string with white spaces String str = " "; // check if str1 is null or empty System.out.println("str is " + isNullEmpty(str)); } // method check if string is null or empty public static String isNullEmpty(String str) { // check if string is null if (str == null) { return "NULL"; } // check if string is empty else if (str.trim().isEmpty()){ return "EMPTY"; } else { return "neither NULL nor EMPTY"; } } }