Given a string str, the task is to check if this string contains only whitespaces or some text, in Java.
Input: str = " " Output: True Input: str = "GFG" Output: False
- 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.
- 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
- Combine the use of both methods using Method Chaining.
- Print true if the above condition is true. Else print false.
Below is the implementation of the above approach:
Is string [GeeksforGeeks] only whitespaces? false Is string [ ] only whitespaces? true
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java program to check whether a string is a Palindrome
- Program to check if the String is Empty in Java
- Program to check if the String is Null in Java
- Java | Removing whitespaces using Regex
- C Program to Check if a Given String is Palindrome
- Program to check if first and the last characters of string are equal
- Program to check if input is an integer or a string
- Python program to check if a string is palindrome or not
- Python program to check if given string is pangram
- Python program to check if a string contains all unique characters
- Python program to check if given string is vowel Palindrome
- How to check if string contains only digits in Java
- Check for string rotation in Java
- Check if a string contains only alphabets in Java using Regex
- Check if a String starts with any of the given prefixes in Java
- Check if a string contains only alphabets in Java using ASCII values
- Check if a string contains only alphabets in Java using Lambda expression
- Java program to check palindrome (using library methods)
- Java Program to get a character from a String
- Java program to expand a String if range is given?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.