StringUtils中的isEmpty 和 isBlank

一、导入字符串工具类Maven依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

org.apache.commons.lang3.StringUtils(Apache Commons Lang)和 org.springframework.util.StringUtils(Spring Framework)是两种不同的字符串工具类,功能有部分重叠,前者提供 100+ 方法,覆盖字符串的空值检查、截取、拼接、转换、比较、格式化等。功能更全面。

二、StringUtils.isEmpty()

作用是判断是否为空.。空格,并不是严格的空值

package com.example.springboottest;

import org.apache.commons.lang3.StringUtils;
public class Test {
    public static void main(String[] args) {
        System.out.println(StringUtils.isEmpty(null));//true
        System.out.println(StringUtils.isEmpty(""));//true
        System.out.println(StringUtils.isEmpty(" "));//false
    }
}

ctrl+B溯源查看底层源码
在这里插入图片描述

三、StringUtils.isBlank()

空格也为true

package com.example.springboottest;

import org.apache.commons.lang3.StringUtils;
public class Test {
    public static void main(String[] args) {
        System.out.println(StringUtils.isBlank(null));//true
        System.out.println(StringUtils.isBlank(""));//true
        System.out.println(StringUtils.isBlank(" "));//true
        System.out.println(StringUtils.isBlank("  "));//true
        System.out.println(StringUtils.isBlank("abc"));//false
        System.out.println(StringUtils.isBlank(" abc "));//false
    }
}

ctrl+B溯源查看底层源码
在这里插入图片描述

四、StringUtils的其他方法

package com.example.springboottest;

import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
public class Test {
    public static void main(String[] args) {
        System.out.println(StringUtils.substring("abc", 1)); // "bc"
        System.out.println(StringUtils.leftPad("1", 3, '0')); // "001"
        List<String> list = Arrays.asList("a", "b", "c");
        System.out.println(StringUtils.join(list, ","));  // "a,b,c"
    }
}

可以参考官方的文档
链接: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值