如何检查 Java 字符串是否全部为空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3247067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:45:19  来源:igfitidea点击:

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

javastringwhitespace

提问by Ankur

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

我想使用 Java 检查 Java 字符串或字符数组是否不只是由空格组成?

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

这是一个非常相似的问题,除了它是 Javascript:
如何检查字符串是否包含字符和空格,而不仅仅是空格?

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

编辑:我删除了有关字母数字字符的部分,因此更有意义。

采纳答案by Carl Smotricz

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:

这仅检查(非)空白。如果要检查特定字符类,则需要使用match()带有正则表达式的mighty ,例如:

if (string.matches(".*\w.*")) ...

...which checks for at least one (ASCII) alphanumeric character.

...检查至少一个(ASCII)字母数字字符。

回答by Corey Ogburn

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()

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.

返回字符串的副本,省略前导和尾随空格。如果此 String 对象表示一个空字符序列,或者此 String 对象表示的字符序列的第一个和最后一个字符的代码都大于 '\u0020'(空格字符),则返回对此 String 对象的引用。

否则,如果字符串中没有代码大于 '\u0020' 的字符,则创建并返回一个表示空字符串的新 String 对象。

否则,令 k 为代码大于 '\u0020' 的字符串中第一个字符的索引,并令 m 为代码大于 '\u0020' 的字符串中最后一个字符的索引。创建一个新的String对象,表示这个字符串的子串,以索引k处的字符开始,以索引m处的字符结束——即this.substring(k, m+1)的结果。

此方法可用于从字符串的开头和结尾修剪空格;事实上,它还修剪了所有 ASCII 控制字符。

返回: 此字符串的副本,其中删除了前导和尾随空格,或者此字符串,如果它没有前导或尾随空格。前导或尾随空格。

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

您可以修剪然后与空字符串进行比较,或者可能检查长度是否为 0。

回答by Chris J

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:

我会使用 Apache Commons Lang 库。它有一个名为 StringUtils 的类,可用于各种字符串操作。要检查字符串是否全部为空格,您可以使用以下命令:

StringUtils.isBlank(<your string>)

Here is the reference: StringUtils.isBlank

这是参考:StringUtils.isBlank

回答by Andreas Dolk

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.

这个答案更侧重于旁注“即至少有一个字母数字字符”。除此之外,它不会给其他(早期的)解决方案添加太多内容,除非它不会在字符串为null.

We want falseif (1) s is nullor (2) s is empty or (3) s only contains whitechars.

我们想要false如果 (1) snull或 (2) s 为空或 (3) s 只包含 whitechars。

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

回答by OscarRyz

Alternative:

选择:

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

回答by redslime

Slightly shorter than what was mentioned by Carl Smotricz:

比 Carl Smotricz 提到的略短:

!string.trim().isEmpty();

回答by Rob Raisch

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

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

注意使用反斜杠 cap-S,意思是“非空白字符”

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

我敢打赌这是最简单的(也许是最快的?)解决方案。

回答by Arun

If you are only checking for whitespace and don't care about null then you can use org.apache.commons.lang.StringUtils.isWhitespace(String str),

如果您只检查空格而不关心空值,那么您可以使用 org.apache.commons.lang.StringUtils.isWhitespace(String str),

StringUtils.isWhitespace(String str);

StringUtils.isWhitespace(String str);

(Checks if the String contains only whitespace.)

(检查字符串是否仅包含空格。)

If you also want to check for null(including whitespace) then

如果您还想检查空值(包括空格),那么

StringUtils.isBlank(String str);

回答by andreyro

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

trim() 和其他提到的正则表达式不适用于所有类型的空格

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

即:Unicode 字符“行分隔符” http://www.fileformat.info/info/unicode/char/2028/index.htm

Java functions Character.isWhitespace() covers all situations.

Java 函数 Character.isWhitespace() 涵盖所有情况。

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

这就是为什么 应该使用已经提到的解决方案 StringUtils.isWhitespace( String ) / 或 StringUtils.isBlank(String) 的原因