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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java | Removing whitespaces using Regex
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Check if a string contains only alphabets in Java using Regex
- How to check if string contains only digits in Java
- Check if a string contains only alphabets in Java
- Python program to check if a string contains all unique characters
- Check if a string contains a palindromic sub-string of even length
- Check if the string contains consecutive letters and each letter occurs exactly once
- Check if a binary string contains all permutations of length k
- Check if a binary string contains consecutive same or not
- Check if a string contains two non overlapping sub-strings "geek" and "keeg"
- Check if given string contains all the digits
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Check if a String contains Anagrams of length K which does not contain the character X
- Check if a String contains any index with more than K active characters
- String matching where one string contains wildcard characters
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Check if a string consists only of special characters
- Java String contains() method with example