且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-15 15:43:10

我能想到的最短的解决方案:

Shortest solution I can think of:

if (string.trim().length() > 0) ...

这只检查(非)空白.如果你想检查特定的字符类,你需要使用强大的 match() 和一个正则表达式,例如:

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.*")) ...

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

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