是否注意过isEmpty 和 isBlank 区别?

mac2026-02-20  9

程序员的成长之路 互联网/程序员/成长/职场 

来源:http://h5ip.cn/ix9z

前言

org.apache.commons.lang.StringUtils 类提供了 String 的常用操作,最为常用的判空有如下两种 isEmpty(String str) 和 isBlank(String str)。

分析

我们通过源码来分析区别:

public static boolean isEmpty(String str) {    return str == null || str.length() == 0;}public static boolean isNotEmpty(String str) {    return !isEmpty(str);}public static boolean isBlank(String str) {    int strLen;    if (str != null && (strLen = str.length()) != 0) {        for(int i = 0; i < strLen; ++i) {            if (!Character.isWhitespace(str.charAt(i))) {                return false;            }        }        return true;    } else {        return true;    }}public static boolean isNotBlank(String str) {    return !isBlank(str);}

可以看到:

1.StringUtils.isEmpty(String str)判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

2.StringUtils.isBlank(String str)判断某字符串是否为空或长度为 0 或由空白符 (whitespace) 构成

3.StringUtils.isNotEmpty(String str)等价于!isEmpty(String str)

4.StringUtils.isNotBlan(String str)等价于!isBlank(String str)

建议

StringUtils.isBlank(String str) 来执行判空操作,判断的条件更多更具体,特别是进行参数校验时,推荐使用。

另外,你们项目是否有isEmpty和isBlank混用的情况?

往期精彩回顾

朕已阅 

最新回复(0)