一、commons-lang3总结
该工具类在以下几个方面,提供了诸多的工具类方法,真的yyds;
- 字符串工具类StringUtils
- 数组工具类ArrayUtils
- 字符工具类CharUtils
- 随机字符工具类RandomStringUtils
- 数字工具类NumberUtils
- 日期工具类DateUtils
1. Maven引入
<!--工具库--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</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");//---trueStringUtils.endsWithIgnoreCase("ABCDEF", "def");//---trueStringUtils.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");//---trueStringUtils.endsWithIgnoreCase("ABCDEF", "def");//---trueStringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true//检查起始字符串是否匹配StringUtils.startsWith("abcdef", "abc");//---trueStringUtils.startsWithIgnoreCase("ABCDEF", "abc");//---trueStringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true//判断字符串大写、小写StringUtils.isAllUpperCase("ABC");//---trueStringUtils.isAllLowerCase("abC");//---false//判断字符串数字StringUtils.isNumeric("123");//---trueStringUtils.isNumeric("12 3");//---false (不识别运算符号、小数点、空格……)StringUtils.isNumericSpace("12 3");//---trueStringUtils.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 mStringUtils.mid("abcdefg", 2, 4);//---"cdef"
3. 随机字符工具类RandomStringUtils
//随机生成n位数数字,返回字符串RandomStringUtils.randomNumeric(n);//在指定字符串中生成长度为n的随机字符串(不按顺序)RandomStringUtils.random(n, "abcdefghijk");
4.日期工具类DateUtils
这个类提供的关于日期的一些通用方法,相对来说还是挺丰富的,【精益编程】将有自己的日期处理工具类,将对这个类进行个性化扩充。




