Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check that a Java String is not all whitespaces?

People also ask

How do you find Whitespaces in a string?

You can use charAt() function to find out spaces in string.

How do you check if a string has all spaces in Java?

In order to check if a String has only unicode digits or space in Java, we use the isDigit() method and the charAt() method with decision making statements. The isDigit(int codePoint) method determines whether the specific character (Unicode codePoint) is a digit. It returns a boolean value, either true or false.

Which method is used to check string contains Whitespaces or not?

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.

How do you make sure a string has no spaces?

Check if a string contains no spaces. The \S character class shortcut will match non-white space characters. To make sure that the entire string contains no spaces, use the caret and the dollar sign: ^\S$.


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.


I would use the Apache Commons Lang library. It has a class called StringUtils that is useful for all sorts of String operations. For checking if a String is not all whitespaces, you can use the following:

StringUtils.isBlank(<your string>)

Here is the reference: StringUtils.isBlank


Slightly shorter than what was mentioned by Carl Smotricz:

!string.trim().isEmpty();

StringUtils.isBlank(CharSequence)

https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank-java.lang.CharSequence-


If you are using Java 11 or more recent, the new isBlank string method will come in handy:

!s.isBlank();

If you are using Java 8, 9 or 10, you could build a simple stream to check that a string is not whitespace only:

!s.chars().allMatch(Character::isWhitespace));

In addition to not requiring any third-party libraries such as Apache Commons Lang, these solutions have the advantage of handling any white space character, and not just plain ' ' spaces as would a trim-based solution suggested in many other answers. You can refer to the Javadocs for an exhaustive list of all supported white space types. Note that empty strings are also covered in both cases.


if(target.matches("\\S")) 
    // then string contains at least one non-whitespace character

Note use of back-slash cap-S, meaning "non-whitespace char"

I'd wager this is the simplest (and perhaps the fastest?) solution.


This answer focusses more on the sidenote "i.e. has at least one alphanumeric character". Besides that, it doesn't add too much to the other (earlier) solution, except that it doesn't hurt you with NPE in case the String is null.

We want false if (1) s is null or (2) s is empty or (3) s only contains whitechars.

public static boolean containsNonWhitespaceChar(String s) {
  return !((s == null) || "".equals(s.trim()));
}