StrUtil.isEmpty(str)、StrUtil.isBlank(str)、ObjectUtil.isNull()、Objects.isNull()等判空处理

导入依赖文件:

cn.hutool
hutool-all
5.7.13

字符串判空

1.StrUtil.isEmpty(str)、 StrUtil.isBlank(str)使用字符串判空处理(hutool)
使用StrUtil.isEmpty()方法来判断不同的字符串是否为空。当字符串为null或长度为0时,该方法返回true,否则返回false。

使用StrUtil.isBlank()方法,当字符串为null、空字符串("")或只包含空白字符时,该方法返回true,否则返回false。
案例:

@Test
void hutoolsTest1(){
String str1 = null;
String str3 = “”;
String str4 = " ";

System.out.println(StrUtil.isEmpty(str1)); //字符串为null返回true
System.out.println(StrUtil.isEmpty(str3)); //字符串长度为0返回true
System.out.println(StrUtil.isEmpty(str4)); //字符串长度不为0,且不为null返回false
System.out.println("-------------------------------");
System.out.println(StrUtil.isBlank(str1)); //字符串为null返回true
System.out.println(StrUtil.isBlank(str3)); //字符串长度为0返回true
System.out.println(StrUtil.isBlank(str4)); //字符串为空字符串返回true

}

总结:
StrUtil.isEmpty(str)和StrUtil.isBlank(str)这两个方法在Hutool工具库中都用于判断字符串是否为空,但有一些区别。
1.StrUtil.isEmpty(str):
a.判断字符串是否为空,即为null或长度为0。
b.该方法只关注字符串的长度,不考虑其中是否包含空白字符。
c.如果字符串为null或长度为0,则返回true,否则返回false。
d.例如:StrUtil.isEmpty(null)和StrUtil.isEmpty("")都会返回true。
2.StrUtil.isBlank(str):
a.判断字符串是否为空或只包含空白字符。
b.除了判断字符串的长度外,还会判断其中是否包含空白字符(包括空格、制表符、换行符等)。
c.如果字符串为null、长度为0或只包含空白字符,则返回true,否则返回false。
d.例如:StrUtil.isBlank(null)、StrUtil.isBlank("")和StrUtil.isBlank(" ")都会返回true。
2.使用StrUtil.isNotBlank(str)、StrUtil.isNotEmpty(str)判非空处理(hutool)

@Test
void hutoolsTest2(){

String str1 = null;

String str2 = "a";

String str3 = "";

String str4 = " ";

System.out.println(StrUtil.isNotEmpty(str1)); //字符串为null返回false
System.out.println(StrUtil.isNotEmpty(str2)); //字符串长度不为0,且不为null返回true
System.out.println(StrUtil.isNotEmpty(str3));  //字符串长度为0返回false
System.out.println(StrUtil.isNotEmpty(str4)); //字符串为空字符串返回true

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

System.out.println(StrUtil.isNotBlank(str1)); //字符串为null返回false
System.out.println(StrUtil.isNotBlank(str2)); //字符串长度不为0,且不为null返回true
System.out.println(StrUtil.isNotBlank(str3));  //字符串长度为0返回false
System.out.println(StrUtil.isNotBlank(str4));  //字符串为空字符串返回false

}

3.使用java原生的工具类判空
String类中的isEmpty()
@Test
void nullTest(){
String str1 = null;

    String str2 = "a";

    String str3 = "";

    String str4 = " ";

// System.out.println(str1.isEmpty()); //空指针异常,不能调用空对象的方法
System.out.println(str2.isEmpty()); //空对象,判空false
System.out.println(str3.isEmpty()); //字符串长度为0,true
System.out.println(str4.isEmpty()); //空字符串,false
}

对象判空
1.使用ObjectUtil.isNull()判空 (Huool)
此方法不能判断对象中字段为空的情况,
ObjectUtil.isNull():如果对象为null,则返回true;否则返回false
ObjectUtil.isNotNull():如果对象不为null,则返回true;否则返回false
@Test
void hutoolsTest3(){
Object obj1 = null;
Object obj2 = new Object();
System.out.println(ObjectUtil.isNull(obj1)); //对象为null, true
System.out.println(ObjectUtil.isNull(obj2)); // false
System.out.println("-------------------------------");
System.out.println(ObjectUtil.isNotNull(obj1)); // false
System.out.println(ObjectUtil.isNotNull(obj2)); // true
}

2.使用java原生类判空Objects.isNull()
Objects.isNull():如果对象为null,则返回true;否则返回false
Objects.nonNull():如果对象不为null,则返回true;否则返回false
@Test
void objectNullTest(){
Object obj1 = null;
Object obj2 = new Object();

System.out.println(Objects.isNull(obj1));  // true
System.out.println(Objects.isNull(obj2));  // false


System.out.println(Optional.ofNullable(obj1).isPresent());  // false
System.out.println(Optional.ofNullable(obj2).isPresent());  // true

System.out.println("-------------------------------");
System.out.println(Objects.nonNull(obj1));  // false
System.out.println(Objects.nonNull(obj2));  // true

}

猜你喜欢

转载自blog.csdn.net/qq_52227892/article/details/134930722
今日推荐