How do I check that a Java String is not all whitespaces?
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match()
with a regexp such as:
if (string.matches(".*\\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
How do I check whether input string contains any spaces?
Why use a regex?
name.contains(" ")
That should work just as well, and be faster.
How to detect string which contains only spaces?
To achieve this you can use a Regular Expression to remove all the whitespace in the string. If the length of the resulting string is 0
, then you can be sure the original only contained whitespace. Try this:
var str = " ";if (!str.replace(/\s/g, '').length) { console.log('string only contains whitespace (ie. spaces, tabs or line breaks)');}
How can I check if string contains characters & whitespace, not just whitespace?
Instead of checking the entire string to see if there's only whitespace, just check to see if there's at least one character of non whitespace:
if (/\S/.test(myString)) {
// string is not empty and not just whitespace
}
How can I find whitespace in a String?
For checking if a string contains whitespace use a Matcher
and call its find method.
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
If you want to check if it only consists of whitespace then you can use String.matches
:
boolean isWhitespace = s.matches("^\\s*$");
Checking if there is whitespace between two elements in a String
You would use String.strip()
to remove any leading or trailing whitespace, followed by String.split()
. If there is a whitespace, the array will be of length 2 or greater. If there is not, it will be of length 1.
Example:
String test = " 2 2 ";
test = test.strip(); // Removes whitespace, test is now "2 2"
String[] testSplit = test.split(" "); // Splits the string, testSplit is ["2", "2"]
if (testSplit.length >= 2) {
System.out.println("There is whitespace!");
} else {
System.out.println("There is no whitespace");
}
If you need an array of a specified length, you can also specify a limit to split. For example:
"a b c".split(" ", 2); // Returns ["a", "b c"]
If you want a solution that only uses regex, the following regex matches any two groups of characters separated by a single space, with any amount of leading or trailing whitespace:
\s*(\S+\s\S+)\s*
Related Topics
Converting an Array of Objects to an Array of Their Primitive Types
Issue Using Imageio.Write Jpg File: Pink Background
Why Do We Need Immutable Class
How to Format a Java String with Leading Zero
"Unable to Acquire Application Service" Error While Launching Eclipse
How to Pass Arithmetic Operators to a Method in Java
Java Inetaddress.Getlocalhost(); Returns 127.0.0.1 ... How to Get Real Ip
How to Obtain Mouse Click Coordinates Outside My Window in Java
How to Calculate a Time Span in Java and Format the Output
How to Retrieve Value from Jtextfield in Java Swing
Swing on Osx: How to Trap Command-Q
Parsing Xml with Regex in Java
How to Use Regex in String.Contains() Method in Java
Is Java.Util.Date Using Timezone