- What is StringUtils.containsWhitespace in Java?
- Overview
- Syntax
- Parameters
- Returns
- Check if a String is whitespace, empty («») or null in Java
- Example
- Output
- Example
- Output
- How to Use Regex Whitespace in Java
- What is Regex in Java?
- How to Use Regex Whitespace in Java?
- Method 1: Use Predefined Regex Whitespace with Pattern.matches() Method in Java
- Method 2: Use User-defined Regex Whitespace With Pattern and Matcher class
- Conclusion
- About the author
- Farah Batool
What is StringUtils.containsWhitespace in Java?
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
Overview
containsWhitespace is a static method of the StringUtils class that is used to check whether a string contains any whitespace or not. A character is classified as whitespace with the help of Character.isWhitespace.
StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.
org.apache.commons commons-lang3 3.12.0
Note: For other versions of the commons-lang package, refer to Maven Repository.
The StringUtils class can be imported as follows:
import org.apache.commons.lang3.StringUtils;
Syntax
public static boolean containsWhitespace(final CharSequence seq)
Parameters
This method takes one parameter, final CharSequence seq , the Character Sequence/String to be checked for whitespace.
Returns
This method returns true if there is whitespace in the string and false otherwise.
Check if a String is whitespace, empty («») or null in Java
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
The following is an example that checks for an empty string.
Example
Output
String is null or empty or whitespace
Let us see another example that checks for whitespace input.
Example
Output
String is null or empty or whitespace
Learning faster. Every day.
- 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.
How to Use Regex Whitespace in Java
Regex or Regular Expression is a set of special characters that combine to form a pattern to search characters in strings. In computer programming and software engineering, learning regex will be very helpful in finding information in any text. All kinds of text search, formatting, and text replacement operations can be carried out using regular expressions.
This tutorial will guide you about using the regex whitespace in Java.
What is Regex in Java?
A Regular Expression or Regex might be as simple as a single character or a complex pattern. It can be created with a string of text and symbols in a specific order. Most of the characters in a regex are letters and typographic symbols. Regex is case-sensitive, so keep that in mind while creating and using it.
How to Use Regex Whitespace in Java?
Although Java does not have any predefined Regular Expression class. However, we can use regular expressions by importing the “java.util.regex” library. It includes some classes such as “Pattern”, which is used for defining a regex pattern, and “Matcher” class which is used to search with the pattern.
There are two methods to use regex whitespace in Java as follows:
-
- Using Pattern.matches() method (use predefined regex)
- Using Pattern and Matcher class (create user-defined regex to match)
Let’s see how these methods will work with regex for whitespace in Java.
Method 1: Use Predefined Regex Whitespace with Pattern.matches() Method in Java
To find whitespaces in a string, there are three common regexes in Java:
-
- \s: It represents a single white space.
- \s+: It indicates multiple white spaces.
- \u0020: It is the Unicode of the white space used as a regex to find whitespace in a text.
We can use these regexes in the static method “matches()” of the “Pattern” class. Pattern class belongs to the “java.util.regex” package. Below is the syntax of Pattern.matches() method is given:
The specified method takes two arguments: the regular expression and the string to match. The first argument “\s” is the regular expression or regex of the white space, and the second argument ” “ is the space in string. It returns either true or false as a boolean value.
Example 1: Use “\s” WhiteSpace Regex
Here, we will use the “\s” regex in the Pattern.matches() method. We will pass a string with no space in the method as a second argument. The method will check the regex and the string and then return a boolean value that will be stored in the “match” variable:
Print the value of the match variable using the “System.out.println()” method:
The value returned by the “Pattern.matches()” method is “false” because the passed string has no space:
Now we will see some other examples to match whitespace with other regexes.
Example 2: Use “\s+” WhiteSpace Regex
In this example, we will pass the “\s+” regex in the “matches()” method to find multiple spaces:
Print the value of the match variable that stores the returned result from the method:
As the second argument contains spaces, the resultant value is displayed as “true”:
Example 3: Use “\u0020” WhiteSpace Regex
Here, we will show you how Unicode is used as a regex in Java. For the specified purpose, we will use the “\u0020” regex as Unicode of the white space:
The Pattern.matches() method will print “true” as a passed string containing white spaces:
Let’s move to the other method to use regex in Java.
Method 2: Use User-defined Regex Whitespace With Pattern and Matcher class
The “Pattern” class is used to define or create a pattern, while the “Matcher” class is utilized to search according to the given pattern. The pattern for a regex can be created with the help of the “compile()” method of the Pattern class. It takes only one parameter, the pattern you want to compile for any purpose.
The Matcher class matches the pattern by using the “matcher()” method. It takes a “string” as the pattern.
There are some predefined regex for whitespaces that we have discussed above, the remaining are listed below:
Now, let’s check out some examples.
Example 1: Use “\\t\\p” WhiteSpace Regex
In this example, we will find out the number of whitespaces by counting them. First, we will create a String “s” and print it out on console:
Next, we will define a pattern “\\t\\p ” that acts as a whitespace regex in Java and is equal to “\s”. After compiling the given pattern, variable “regexPattern” will contain resultant value:
Call the “matcher()” method and pass “s” String:
Create an integer type variable “count” and initialize it with the value “0”:
Count the number of whitespaces that exist in the string by using a “while” loop. The loop will traverse the String and increment the count variable value if it encounters any space:
Lastly, print the value of count to show how many spaces are found in a string:
Example 2: Use “\p” WhiteSpace Regex
Now, we will find the whitespaces in the string by using another pattern “\p ”. This pattern works similar to the “\s” and “\s+” regex:
Now, we call the “matcher()” method and pass “s” String as argument:
As in the above example, we also use a “while” loop to count the spaces in a string and print them:
The given output indicates that our String “Welcome to Linux Hint” contains three whitespaces:
We compile all the easiest methods that can help you to use regex whitespace in Java.
Conclusion
There are many regular expressions for whitespace such as “\s”, “\s+”, “\u0020”, “\\t\\p ”, and “\\p ”. These regexes are used in the matches() method of the Pattern class or by defining a pattern with the Pattern Class and matching it using the Matcher class. The most commonly used regex whitespace is \s and \s+. In this tutorial, we covered all the methods to use regex whitespace in Java.
About the author
Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.