点击上方 Java老铁,并选择 设为星标
优质文章和资料会及时送达
添加依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
2.判空的两种方法
isEmpty()
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
说明:判断是否为空或者为null,是则返回true
举例子:
boolean empty = StringUtils.isEmpty(null);
boolean empty1 = StringUtils.isEmpty("");
boolean empty2 = StringUtils.isEmpty(" ");
boolean empty3 = StringUtils.isEmpty(" alex ");
boolean bob = StringUtils.isEmpty("alex");
运行结果为:
true
true
false
false
false
isBlank()
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
//检查是否有空格
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(cs.charAt(i)) == false)) {
return false;
}
}
return true;
}
说明:判断是否为空、null或者为空格
举例:
boolean empty = StringUtils.isBlank(null);
boolean empty1 = StringUtils.isBlank("");
boolean empty2 = StringUtils.isBlank(" ");
boolean empty3 = StringUtils.isBlank(" alex ");
boolean alex = StringUtils.isBlank("alex");
运行结果:
true
true
true
false
false
上述两个方法对应的判断非空方法分别是:isNotEmpty()h 和 isNotBlank(),非空返回true。
往
期
回
顾
1.重磅资料,微服务
2.快速了解缓存穿透与缓存雪崩
3.idea的常用设置和快捷键(内含动图)
5.计算机网络(一)
关注我
获取更多
Java干货
原创文章
视频资料
技术交流群