utils002_字符串的非空判断

本文介绍了在Java中进行字符串操作时,可以使用的两个实用库:Apache Commons Lang的StringUtils和Hutool的StrUtil。通过示例代码展示了如何判断字符串是否为空、至少有一个为空、没有一个空以及全部为空的方法,帮助开发者选择合适的字符串处理工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.jingsong.test;

import org.apache.commons.lang3.StringUtils;

/**
 * @author jingsong
 * @date 2022/4/20 0:03
 * @description
 *      对于字符串的操作推荐使用下面的两个jar包
 *        1. StringUtils apache出品
 *          https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
 *        2. StrUtil 中文注释,功能强大
 *          https://mvnrepository.com/artifact/cn.hutool/hutool-all
 */
public class StringTest {
    public static void main(String[] args) {
        String  a = "",
                b = " ",
                c = "hehe",
                d = "  Q  ",
                e = null;
        // 1. empty是判断是否为空,blank是判断是否有文字
        System.out.println(StringUtils.isEmpty(b));// false
        System.out.println(StringUtils.isBlank(b));// true
        System.out.println();

        // 2,3,4 中的方法可以同时判断多个,更加方便
        // 2. 至少有一个为空
        System.out.println(StringUtils.isAnyEmpty(a, c, d, e));// true
        System.out.println(StringUtils.isAnyEmpty(b, c, d));// false
        System.out.println();

        // 3. 没一个空的
        System.out.println(StringUtils.isNoneEmpty(a,c));// false
        System.out.println(StringUtils.isNoneEmpty(b, c, d));// true
        System.out.println();

        // 4. 都是空的
        System.out.println(StringUtils.isAllEmpty(a, e));// true
        System.out.println(StringUtils.isAllEmpty(a, b, c, d));// false
        System.out.println();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值