首先要利用StringUtils类的方法,需要调用
import org.apache.commons.lang3.StringUtils;
因此需要引用org.apache.commons.lang3包。
Maven下的引用
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
Gradle引用
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8'
区别
isNotEmpty(str)等价于 str != null && str.length > 0
isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length > 0
同理
isEmpty 等价于 str == null || str.length == 0
isBlank 等价于 str == null || str.length == 0 || str.trim().length == 0
str.length > 0 && str.trim().length > 0 ---> str.length > 0
示例

使用commons-lang3进行字符串校验
本文介绍了如何使用Apache Commons Lang3库中的StringUtils来进行字符串的空值和空白检查,并提供了Maven和Gradle的依赖引入方式。通过isNotEmpty、isNotBlank等方法,可以有效地判断字符串是否为空或空白。
462

被折叠的 条评论
为什么被折叠?



