commons-lang3使用总结

一、commons-lang3总结
该工具类在以下几个方面,提供了诸多的工具类方法

字符串工具类StringUtils
数组工具类ArrayUtils
字符工具类CharUtils
随机字符工具类RandomStringUtils
数字工具类NumberUtils
日期工具类DateUtils

  1. Maven引入
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>最新</version>
</dependency>

2.字符串工具类StringUtils

//缩短到某长度,用...结尾.等价于(substring(str, 0, max-3) + "...")
//public static String abbreviate(String str,int maxWidth)
StringUtils.abbreviate("123456789", 6);// ---"123..."
//字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
StringUtils.appendIfMissing("abc","xyz");//---"abcxyz"
StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz");//---"abcXYZ"
//首字母大小写转换
StringUtils.capitalize("dog");//---"Dog"
StringUtils.uncapitalize("Dog");//---"dog"
//检查字符串结尾后缀是否匹配
StringUtils.endsWith("abcdef", "def");//---true
StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
//去除字符串中的"\n", "\r", or "\r\n"
StringUtils.chomp("abc\r\n");//---"abc"
//判空
StringUtils.isEmpty("abc");
StringUtils.isNotEmpty("abc");
//判空的时候,会去除字符串中的空白字符,比如空格、换行、制表符
StringUtils.isBlank("abc");
StringUtils.isNotBlank("abc");
//统计一字符串在另一字符串中出现次数
StringUtils.countMatches("abba", "a");//---2
//删除字符串中的梭有空格
StringUtils.deleteWhitespace("   ab  c  ");//---"abc"
//比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
StringUtils.difference("abcde", "abxyz");//---"xyz"
//检查字符串结尾后缀是否匹配
StringUtils.endsWith("abcdef", "def");//---true
StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
//检查起始字符串是否匹配
StringUtils.startsWith("abcdef", "abc");//---true
StringUtils.startsWithIgnoreCase("ABCDEF", "abc");//---true
StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
//判断字符串大写、小写
StringUtils.isAllUpperCase("ABC");//---true
StringUtils.isAllLowerCase("abC");//---false
//判断字符串数字
StringUtils.isNumeric("123");//---true
StringUtils.isNumeric("12 3");//---false (不识别运算符号、小数点、空格……)
StringUtils.isNumericSpace("12 3");//---true
StringUtils.isNumericSpace("12 .3");//---false(小数点不算)
//大小写转换
StringUtils.upperCase("aBc");//---"ABC"
StringUtils.lowerCase("aBc");//---"abc"
StringUtils.swapCase("The dog has a BONE");//---"tHE DOG HAS A bone"
//替换字符串内容(多组指定替换ab->w,d->t)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
                new String[]{"w", "t"});//---"wcte"
//重复字符
StringUtils.repeat('a', 3);//---"aaa"
//反转字符串
StringUtils.reverse("bat");//---"tab"
//删除某字符
StringUtils.remove("queued", ‘u‘);//---"qeed"
//left、right从左(右)开始截取n位字符
StringUtils.left("abc", 2);//---"ab"
StringUtils.right("abc", 2);//---"bc"
//从第n位开始截取m位字符 n  m
StringUtils.mid("abcdefg", 2, 4);//---"cdef"
  1. 随机字符工具类RandomStringUtils
//随机生成n位数数字,返回字符串
RandomStringUtils.randomNumeric(n);
//在指定字符串中生成长度为n的随机字符串(不按顺序)
RandomStringUtils.random(n, "abcdefghijk");@[TOC](这里写自定义目录标题)

4.日期工具类DateUtils
这个类提供的关于日期的一些通用方法,相对来说还是挺丰富的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值