为什么我的StringUtils类没有isBlank()

时间:2019-03-23 09:33:12

标签: java

我在JDK中找不到StringUtils.isBlank()

人们在Google上说StringUtils.isBlank()可用于检测空白字符串。但是我的IntelliJ告诉我该功能不存在。但是,StringUtils.isEmpty也不存在。但是我看到很多人正在使用它。我可能做错了什么?

我正在使用IntelliJ,该版本于2019年1月9日在Windows,JDK 12上发布。也已在JDK 9上尝试过。

3 个答案:

答案 0 :(得分:2)

您可以使用自StringJDK11

开始添加的新String.isBlank()类函数

文档here

isBlank
public boolean isBlank()

Returns true if the string is empty or contains only white space codepoints, otherwise false.
Since:
11

另外,根据您的用例,建议您进行以下操作:StringUtils.isBlank()VsString.isEmpty()

答案 1 :(得分:1)

您要使用的库是org.apache.commons.lang.StringUtils,您必须手动将其添加到类路径中,或者将其作为对gradle配置或正在使用的任何其他构建工具的依赖项添加。

IntelliJ不了解该库,因为它不是JDK的一部分。

答案 2 :(得分:0)

StringUtils来自称为“ apache commons”的第三方库;它不是Java开箱即用包含的库的一部分,您必须手动添加它。

但是没有必要。从JDK11开始,字符串本身具有isBlank方法:

System.out.println("   ".isBlank());

将打印true。也许您读了一些有关isBlank的内容,并将这个新的(JDK11中引入的)API与apache commons库中的实用程序方法相混淆。

相关问题