String判空工具类总结

本文介绍了在Java开发中常用的三个字符串工具类:Spring的StringUtils、Apache Commons Lang3的StringUtils以及Hutool的StrUtil。通过示例代码展示了它们在判断字符串是否为空时的不同行为,并对这三个工具类进行了优缺点分析。建议在项目中根据具体需求和已引入的依赖选择合适的工具类。
摘要由CSDN通过智能技术生成

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比较多

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值