- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a String is whitespace, empty ("") or null in Java
Let’s say the following is our string.
String myStr = "";
Now, we will check whether the above string is whitespace, empty ("") or null.
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"); }
The following is an example that checks for an empty string.
Example
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"); } } }
Output
String is null or empty or whitespace
Let us see another example that checks for whitespace input.
Example
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"); } } }
Output
String is null or empty or whitespace
- Related Articles
- Check if a String is empty ("") or null in Java
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- Check if a String is not empty ("") and not null in Java
- C# Program to check a string for whitespace characters or null
- Java Program to check if a string is empty or not
- How to check if field is null or empty in MySQL?
- How do I check if a column is empty or null in MySQL?
- Check whether a field is empty or null in MySQL?
- How to check if String is empty in Java?
- Checking for Null or Empty or White Space Only String in Java.
- How to test String is null or empty?
- Java program to find whether the given string is empty or null
- Which one is better in MySQL - NULL or empty string?
- Checking for Null or Empty in Java.

Advertisements