String判空工具类总结
说明:
StringUtils类似的工具类有很多,包括Spring支持的,apche的,还有自定义的,接下来我总结下我日常遇到过的StringUtils,还有这些api我特别容易搞混,所以这次整理记录一下
直接上代码
package com.example.demo;
import cn.hutool.core.util.StrUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;
/**
* @author shaoming
* @Date: 2021/4/23 16:47
* @Description:
*/
@SpringBootTest
public class StringUitilTest {
/**
几个针对性的测试的字符窜
*/
@Test
public void testCreateString() {
String str1 = "";
String str2 = " ";
String str3 = null;
String str4 = " 我是string的内容 ";
}
/**
* 测试org.springframework.util.StringUtils工具判断字符窜是否为空的api
*/
@Test
public void testStringNotEmptyBySpringUtils() {
String str1 = "";
String str2 = " ";
String str3 = null;
String str4 = " 我是string的内容 ";
boolean empty1 = StringUtils.isEmpty(str1);
boolean empty2 = StringUtils.isEmpty(str2);
boolean empty3 = StringUtils.isEmpty(str3);
boolean empty4 = StringUtils.isEmpty(str4);
System.out.println(empty1);
System.out.println(empty2);
System.out.println(empty3);
System.out.println(empty4);
/*
控制台打印:
true
false
true
false
*/
}
/**
* org.apache.commons.lang3.StringUtils这个工具类进行字符窜判空的工具类
* 说明:
* 使用这个工具类需要引入以下依赖
* <dependency>
* <groupId>org.apache.commons</groupId>
* <artifactId>commons-lang3</artifactId>
* <version>3.1</version>
* </dependency>
*/
@Test
public void testStirngNotEmptyByapacheCommonsLangsStringUtils() {
String str1 = "";
String str2 = " ";
String str3 = null;
String str4 = " 我是string的内容 ";
System.out.println("测试isBlank方法");
System.out.println(org.apache.commons.lang3.StringUtils.isBlank(str1));
System.out.println(org.apache.commons.lang3.StringUtils.isBlank(str2));
System.out.println(org.apache.commons.lang3.StringUtils.isBlank(str3));
System.out.println(org.apache.commons.lang3.StringUtils.isBlank(str4));
System.out.println("测试isEmpty方法");
System.out.println(org.apache.commons.lang3.StringUtils.isEmpty(str1));
System.out.println(org.apache.commons.lang3.StringUtils.isEmpty(str2));
System.out.println(org.apache.commons.lang3.StringUtils.isEmpty(str3));
System.out.println(org.apache.commons.lang3.StringUtils.isEmpty(str4));
/*
* 控制台打印
* 测试isBlank方法
* true
* true
* true
* false
* 测试isEmpty方法
* true
* false
* true
* false
*/
}
/**
* 测试hutool的String判空工具类
* 说明:
* 使用StrUtil需要在mavne项目中引入hutool的依赖
* <dependency>
* <groupId>cn.hutool</groupId>
* <artifactId>hutool-all</artifactId>
* <version>5.6.3</version>
* </dependency>
*/
@Test
public void testStringByHutoolofStrUtil() {
String str1 = "";
String str2 = " ";
String str3 = null;
String str4 = " 我是string的内容 ";
System.out.println("测试hutool中String判空的isBlank方法");
System.out.println(StrUtil.isBlank(str1));
System.out.println(StrUtil.isBlank(str2));
System.out.println(StrUtil.isBlank(str3));
System.out.println(StrUtil.isBlank(str4));
System.out.println("测试hutool中String判空的isEmpty方法");
System.out.println(StrUtil.isEmpty(str1));
System.out.println(StrUtil.isEmpty(str2));
System.out.println(StrUtil.isEmpty(str3));
System.out.println(StrUtil.isEmpty(str4));
/*
* 控制台打印
* 测试hutool中String判空的isBlank方法
* true
* true
* true
* false
* 测试hutool中String判空的isEmpty方法
* true
* false
* true
* false
*/
}
}
总结
1.如果我们可以使用hutool工具类,我们可以使用hutool,因为这个开源工具库已经成熟,而且它的api比较多,而且社区很活跃
2.StrUtil工具类名称容易区分和记住,别的框架的string工具类都交StringUtils等,很容易混淆,而且api容易记错
3.如果是内网开发,一般项目中都会有apache的StringUtils,我们可以使用apache的StringUtils,它的api比较多