判断String为空 StringUtils工具 isNotEmpty与isNotBlank区别

如何判断String是否为空?

判断Strings是否为空,很多人第一反应就是 str != null && str.length > 0。可能已经忘了StringUtils工具包了。
工具包中有 isNotEmpty 和isNotBlank 两个方法,都可以判断String是否为空,区别在与,在判断空白字符时,isNotBlank为false,而isNotEmp为ture。

isNotEmpty :

判断某字符串是否非空

StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty(“bob”) = true

isNotBlank:

判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成,
下面是示例:

StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" “) = false
StringUtils.isNotBlank(”\t \n \f \r") = false
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

猜你喜欢

转载自blog.csdn.net/qq_43842093/article/details/123909223