views:

533

answers:

7

I want to check that Java String/character array is not just made up of whitespaces. How would I go about doing this in Java.

This is a very similar question except it's Javascript http://stackoverflow.com/questions/2031085/check-if-string-contains-characters-whitespaces-not-only-whitespaces

EDIT: I removed the bit about alphanumeric characters so it makes more sense.

+20  A: 

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.

Carl Smotricz
+1 for match(), such a useful function
northpole
FWIW: I would expect the first solution to be considerably faster.
Stephen C
@Stephen C: Absolutely! But as @Uri pointed out, I'm having to solve two different problems thanks to the ambiguity of the question :) Also, I rarely use `matches()`: for performance, I usually store the `Pattern` in a `final static`. Pays off if the same code runs frequently.
Carl Smotricz
@Carl - look out, there's one `string == null` coming! - Ouch - NPE - that hurts ;-))
Andreas_D
@Andreas_D: Heh, I got my orders! The OP said he wanted to check a string or char array, he never said anything about nulls! :) \*checks the fine print in the contract\* "`null` is not a string!"
Carl Smotricz
Thanks, the first solution works for me.
Ankur
+4  A: 

StringUtils.isBlank( String )

http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/StringUtils.html

darelf
+1  A: 

The trim method should work great for you.

http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#trim()

Returns a copy of the string, with leading and trailing whitespace omitted. If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1).

This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.

Returns: A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.leading or trailing white space.

You could trim and then compare to an empty string or possibly check the length for 0.

Corey Ogburn
+9  A: 

I would use the apache commons-lang library. It has an object called StringUtils that is usefully for all sorts of String operations. For checking if a String is not all whitespace, you can use the following:

StringUtils.isBlank(<your string>)

Here is the reference: StringUtils.isBlank

Chris J
I prefer this solution compared to using the chosen answer. This will also check for string == null
Richard
+1  A: 

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()));
}
Andreas_D
I think you meant `"".equals(s.trim())`, not `"".equals(s).trim()`.
Justin Ardini
@Justin - thanks a lot! corrected.
Andreas_D
A: 

Alternative:

boolean isWhiteSpaces( String s ) {
    return s != null && s.matches("\\s*");
 }
OscarRyz