How do I check that a Java String is not all white

2019-01-04 00:40发布

I want to check that Java String or character array is not just made up of whitespaces, using Java?

This is a very similar question except it's Javascript:
How can I check if string contains characters & whitespace, not just whitespace?

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

13条回答
女痞
2楼-- · 2019-01-04 01:17
StringUtils.isEmptyOrWhitespaceOnly(<your string>)

will check : - is it null - is it only space - is it empty string ""

https://www.programcreek.com/java-api-examples/?class=com.mysql.jdbc.StringUtils&method=isEmptyOrWhitespaceOnly

查看更多
够拽才男人
3楼-- · 2019-01-04 01:20

trim() and other mentioned regular expression do not work for all types of whitespaces

i.e: Unicode Character 'LINE SEPARATOR' http://www.fileformat.info/info/unicode/char/2028/index.htm

Java functions Character.isWhitespace() covers all situations.

That is why already mentioned solution StringUtils.isWhitespace( String ) /or StringUtils.isBlank(String) should be used.

查看更多
虎瘦雄心在
4楼-- · 2019-01-04 01:25

Slightly shorter than what was mentioned by Carl Smotricz:

!string.trim().isEmpty();
查看更多
Animai°情兽
5楼-- · 2019-01-04 01:29

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.

查看更多
该账号已被封号
6楼-- · 2019-01-04 01:32

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.

查看更多
Animai°情兽
7楼-- · 2019-01-04 01:34

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()));
}
查看更多
登录 后发表回答