点击登录,精彩内容等着你

必掌握工具类(1)commons-lang3,业界标准类库,编程加速神器

全栈侠客

2022-09-30
java有挺多的第三方工具类库,十分强大,可以大大简化代码量,提升开发效率。经过多年的广泛使用,早就成为了业界标准类库。commons-lang3就多种类操作提供非常多的使用方法,我们的精益编程中也会大量使用。

一、commons-lang3总结

该工具类在以下几个方面,提供了诸多的工具类方法,真的yyds;

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

1. Maven引入

  1. <!--工具库-->
  2. <dependency>
  3. <groupId>org.apache.commons</groupId>
  4. <artifactId>commons-lang3</artifactId>
  5. <version>3.12.0</version>
  6. </dependency>

2.字符串工具类StringUtils

  1. //缩短到某长度,用...结尾.等价于(substring(str, 0, max-3) + "...")
  2. //public static String abbreviate(String str,int maxWidth)
  3. StringUtils.abbreviate("123456789", 6);// ---"123..."
  4. //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
  5. StringUtils.appendIfMissing("abc","xyz");//---"abcxyz"
  6. StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz");//---"abcXYZ"
  7. //首字母大小写转换
  8. StringUtils.capitalize("dog");//---"Dog"
  9. StringUtils.uncapitalize("Dog");//---"dog"
  10. //检查字符串结尾后缀是否匹配
  11. StringUtils.endsWith("abcdef", "def");//---true
  12. StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
  13. StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
  14. //去除字符串中的"\n", "\r", or "\r\n"
  15. StringUtils.chomp("abc\r\n");//---"abc"
  16. //判空
  17. StringUtils.isEmpty("abc");
  18. StringUtils.isNotEmpty("abc");
  19. //判空的时候,会去除字符串中的空白字符,比如空格、换行、制表符
  20. StringUtils.isBlank("abc");
  21. StringUtils.isNotBlank("abc");
  22. //统计一字符串在另一字符串中出现次数
  23. StringUtils.countMatches("abba", "a");//---2
  24. //删除字符串中的梭有空格
  25. StringUtils.deleteWhitespace(" ab c ");//---"abc"
  26. //比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
  27. StringUtils.difference("abcde", "abxyz");//---"xyz"
  28. //检查字符串结尾后缀是否匹配
  29. StringUtils.endsWith("abcdef", "def");//---true
  30. StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
  31. StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
  32. //检查起始字符串是否匹配
  33. StringUtils.startsWith("abcdef", "abc");//---true
  34. StringUtils.startsWithIgnoreCase("ABCDEF", "abc");//---true
  35. StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
  36. //判断字符串大写、小写
  37. StringUtils.isAllUpperCase("ABC");//---true
  38. StringUtils.isAllLowerCase("abC");//---false
  39. //判断字符串数字
  40. StringUtils.isNumeric("123");//---true
  41. StringUtils.isNumeric("12 3");//---false (不识别运算符号、小数点、空格……)
  42. StringUtils.isNumericSpace("12 3");//---true
  43. StringUtils.isNumericSpace("12 .3");//---false(小数点不算)
  44. //大小写转换
  45. StringUtils.upperCase("aBc");//---"ABC"
  46. StringUtils.lowerCase("aBc");//---"abc"
  47. StringUtils.swapCase("The dog has a BONE");//---"tHE DOG HAS A bone"
  48. //替换字符串内容(多组指定替换ab->w,d->t)
  49. StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
  50. new String[]{"w", "t"});//---"wcte"
  51. //重复字符
  52. StringUtils.repeat('a', 3);//---"aaa"
  53. //反转字符串
  54. StringUtils.reverse("bat");//---"tab"
  55. //删除某字符
  56. StringUtils.remove("queued", u‘);//---"qeed"
  57. //left、right从左(右)开始截取n位字符
  58. StringUtils.left("abc", 2);//---"ab"
  59. StringUtils.right("abc", 2);//---"bc"
  60. //从第n位开始截取m位字符 n m
  61. StringUtils.mid("abcdefg", 2, 4);//---"cdef"

3. 随机字符工具类RandomStringUtils

  1. //随机生成n位数数字,返回字符串
  2. RandomStringUtils.randomNumeric(n);
  3. //在指定字符串中生成长度为n的随机字符串(不按顺序)
  4. RandomStringUtils.random(n, "abcdefghijk");

4.日期工具类DateUtils

这个类提供的关于日期的一些通用方法,相对来说还是挺丰富的,【精益编程】将有自己的日期处理工具类,将对这个类进行个性化扩充。

阅读 1060     最后编辑 2022-09-30 16:40
文章补充
评论(0) 发表新评论
  • ...暂无评论...

我是有底线的 评论与点赞5分钟更新一次
回复评论
取消关闭

请先登录