Program to check if a String in Java contains only whitespaces Last Updated : 22 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string str, the task is to check if this string contains only whitespaces or some text, in Java. Examples: Input: str = " " Output: True Input: str = "GFG" Output: False Approach: Get the String to be checked in str We can use the trim() method of String class to remove 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. If the string contained only whitespaces, then this method will return true Syntax: str.isEmpty() Combine the use of both methods using Method Chaining. str.trim().isEmpty(); Print true if the above condition is true. Else print false. Below is the implementation of the above approach: Java // Java Program to check if // the String is not all whitespaces class GFG { // Function to check if the String is all whitespaces public static boolean isStringAllWhiteSpace(String str) { // Remove the leading whitespaces using trim() // and then check if this string is empty if (str.trim().isEmpty()) return true; else return false; } // Driver code public static void main(String[] args) { String str1 = "GeeksforGeeks"; String str2 = " "; System.out.println("Is string [" + str1 + "] only whitespaces? " + isStringAllWhiteSpace(str1)); System.out.println("Is string [" + str2 + "] only whitespaces? " + isStringAllWhiteSpace(str2)); } } Output: Is string [GeeksforGeeks] only whitespaces? false Is string [ ] only whitespaces? true Comment More infoAdvertise with us Next Article Program to check if the String is Empty in Java C code_r Follow Improve Article Tags : Java Java-String-Programs Practice Tags : Java Similar Reads Check if a String Contains only Alphabets in Java using Regex Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating, and editing a text. It is widely used to define a constraint on strings such as a password. Regular Expressions are provided under java.util.regex package. For any string, here the task 2 min read Check if a String Contains only Alphabets in Java In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements.Example: The most common and straightforward approach to validate if 4 min read Program to check if the String is Empty in Java Given a string str, the task is to check if this string is empty or not, in Java. Examples: Input: str = "" Output: True Input: str = "GFG" Output: False Approach 1: Get the String to be checked in strWe can simply check if the string is empty or not using the isEmpty() method of String class Syntax 2 min read Check if a String Contains Only Alphabets in Java Using Lambda Expression Lambda expressions basically express instances of functional interfaces (An interface with a single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces. Given a String 3 min read Removing whitespaces using Regex in Java Given a string, your task is to remove the whitespaces in the string using Java Regex (Regular Expressions). Examples Input : Hello Everyone . Output : HelloEveryone. Input : Geeks for Geeks . Output : GeeksforGeeks.Regular Expressions Regular Expressions or Regex is an API for defining String patte 3 min read String Handling with Apache Commons' StringUtils Class in Java The Apache Commons Lang library is a popular third-party library for working with Strings in Java, and the StringUtils class is a key part of this library. StringUtils is a utility class that provides a wide range of String manipulation methods that are not available in the standard Java String clas 9 min read Like