commons-lang3工具常用方法

commons-lang3是一个开源的Java类库,提供了一组实用工具类,主要用于操作字符串、日期、数值、数组等基本类型和对象。它的功能非常全面,几乎可以满足Java程序开发中所有涉及到的常规任务。

引入依赖

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

 

字符串处理

判断字符串是否为null或者空白字符

       // null
        String str=null;
        // 空字符串
        String str1="   ";
        // str
        // 判断字符串是否非为null或者空白字符
        System.out.println(StringUtils.isBlank(str)); //true
        // 判断字符串是否非为null或者空白字符
        System.out.println(StringUtils.isNoneBlank(str)); //false
        // str1
        // 判断字符串是否为null或者空白字符
        System.out.println(StringUtils.isBlank(str1)); //true
        // 判断字符串是否非为null或者空白字符
        System.out.println(StringUtils.isNoneBlank(str1)); //false

判断字符串是否为null

        // null
        String str=null;

        // str
        // 判断字符串是否非为null
        System.out.println(StringUtils.isBlank(str)); //true
        // 判断字符串是否非为null
        System.out.println(StringUtils.isNoneBlank(str)); //false

截取字符串

        // str
        String str="13825339078";
        
        // 截取字符串左边3个字符
        String left = StringUtils.left(str, 3); // 138
        // 截取字符串右边4个字符
        String right = StringUtils.right(str, 4); // 9078
        // 填充字符串左边字符使整个字符串长度为7
        String leftPad = StringUtils.leftPad(left, 7, '*'); // ****138
        // 填充字符串右边字符使整个字符串长度为7
        String rightPad = StringUtils.rightPad(left, 7, '*');
        // 脱敏
        System.out.println(rightPad+right ); // 138****9078

字符串大小写转换

        // str
        String str="wsWS";

        System.out.println(StringUtils.upperCase(str)); // WSWS
        System.out.println(StringUtils.lowerCase(str)); // wsws

数字处理

判断数字 

        // str
        String str="12";
        // isDigits只能判断整数
        System.out.println(NumberUtils.isDigits(str)); // true
        str="12.33";
        // isParsable可以判断时尚芭莎整数、浮点数
        System.out.println(NumberUtils.isParsable(str)); // true
        str="-12.33";
        // isCreatable可以判断是不是整数、浮点数、正负号
        System.out.println(NumberUtils.isCreatable(str)); // true

随机生成数

        int min=0,max=2000;
        System.out.println(RandomUtils.nextInt(min, max)); // 626

对象处理

返回第一个值 

        Integer a=null;
        Integer b=null;
        Integer c=1;
        System.out.println(ObjectUtils.firstNonNull(a, b, c)); // 1

数组处理

判断数组是否为null或为长度为0 

        Integer[] a=new Integer[0];
        System.out.println(ArrayUtils.isEmpty(a));
        a=null;
        System.out.println(ArrayUtils.isEmpty(a));

向已有数组追加

        Integer[] a=new Integer[1];
        a[0]=3;
        System.out.println(ArrayUtils.toString(a)); // {3}
        Integer[] b = ArrayUtils.add(a, 5);
        System.out.println(ArrayUtils.toString(b)); // {3,5}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值