一、进入到阿帕奇官网https://apache.org/
二、翻到最下面选择commons
下拉选择Lang
点击Download选择commons-lang3-3.12.0-bin.zip
解压后拿到这个文件
点击Idea点击file->Project Structure
点击模块—》加号—》找到刚才的文件链接进去点击Apply重启一下idea就可以开始使用了
下面几行代码演示可供参考
// upperCase--全部转大写
System.out.println(StringUtils.upperCase(null)); // null
System.out.println(StringUtils.upperCase("china")); // CHINA(全部转为大写)
System.out.println(StringUtils.upperCase("china", Locale.ENGLISH)); // CHINA(按照制定规则转换为大写)
System.out.println();
// lowerCase--全部转小写
System.out.println(StringUtils.lowerCase(null)); // null
System.out.println(StringUtils.lowerCase("CHINA")); // china(全部转为小写)
System.out.println(StringUtils.lowerCase("CHINA",Locale.ENGLISH)); // china(按照制定规则转换为小写)
System.out.println();
// swapCase--大小写互换
System.out.println(StringUtils.swapCase(null)); // null
System.out.println(StringUtils.swapCase("chINA")); // CHina
System.out.println();
// 判断字符串是否全部是大写或小写(空或空白符均为false)
System.out.println(StringUtils.isAllUpperCase(null)); // false
System.out.println(StringUtils.isAllUpperCase("")); // false
System.out.println(StringUtils.isAllUpperCase(" ")); // false
System.out.println(StringUtils.isAllUpperCase("CHINA")); // true
System.out.println(StringUtils.isAllLowerCase(null)); // false
System.out.println(StringUtils.isAllLowerCase("")); // false
System.out.println(StringUtils.isAllLowerCase(" ")); // false
System.out.println(StringUtils.isAllLowerCase("china")); // true
System.out.println();
// 常用的方法有:
// 移除单个字符
// StringUtils.remove(String str,char remove);
// 移除单个字符
// StringUtils.remove(String str,String remove);
// 移除开头/结尾匹配的字符序列
// StringUtils.removeStart(String str,String remove);
// StringUtils.removeStartIgnoreCase(String str,String remove);
// StringUtils.removeEnd(String str,String remove);
// StringUtils.removeEndIgnoreCase(String str,String remove);
// 替换单个字符或字符序列
// replace(String text,String searchString,String replacement);
// replace(String text,String searchString,String replacement,int max); // max指替换最大次数, 0:不替换 -1:全部替换
// replaceChars(String str,char searchChar,char replaceChar);
// replaceChars(String str,String searchChars,String replaceChars);
// 只会替换一次,后面即使匹配也不替换
// replaceOnce(String text,String searchString,String replacement);
// 指定位置进行字符串序列替换,从start索引处开始(包含)到end-1索引处为止进行替换
// overlay(String str,String overlay,int start,int end);
// 可以同时替换多个字符序列,一一对应但被替换和替换的字符序列的个数应该对应,否则会报IllegelArgumentException
// replaceEach(String text,String[] searchList,String[] replacementList);
System.out.println(StringUtils.replaceEach("test",new String[] { "t" ,"e"},new String[] { "T" ,"E"})); // TEsT
System.out.println();
// 循环替换--了解即可
// replaceEachRepeatedly(String text,String[] searchList,String[] replacementList);
System.out.println(StringUtils.replaceEachRepeatedly("test",new String[] { "e" , "E"},new String[] {"E" , "Y"})); // tYsT (e被替换为E,E又被替换为Y)
System.out.println();
// 直接反转
System.out.println(StringUtils.reverse("asdfghjkkl")); // lkkjhgfdsa
System.out.println();
// 根据分隔符进行反转
System.out.println(StringUtils.reverseDelimited("a.b.c.d.e.f",'.')); // f.e.d.c.b.a
System.out.println();
// 当指定的截取位置为非负数时,则从左往右开始截取
System.out.println(StringUtils.substring("abcd",1)); // bcd
// 但当索引值为负数时,则从右往左截取,注意此时右侧第一位为-1
System.out.println(StringUtils.substring("abcd",-2)); // cd
System.out.println();
// 指定了起始位置和结束位置,则从起始位置开始截取到结束位置(但不包含结束位置)
System.out.println(StringUtils.substring("abcd",1,3)); // bc
// 特别注意负数时从右到左算的
System.out.println(StringUtils.substring("abcdef",1,-1)); // bcde
System.out.println();
// 从分隔符第一次出现的位置向后或向前截取
System.out.println(StringUtils.substringAfter("abc,de,fg",",")); // de,fg
System.out.println(StringUtils.substringBefore("abc,de,fg",",")); // abc
System.out.println();
// 从分隔符最后一次出现的位置向后或向前截取
System.out.println(StringUtils.substringAfterLast("abc,de,fg",",")); // fg
System.out.println(StringUtils.substringBeforeLast("abc,de,fg",",")); // abc,de
System.out.println();
// 截取指定标记字符串之间的字符序列
// 1.返回searchChar在字符串中第一次出现的位置,如果searchChar没有在字符中出现,则返回-1
System.out.println(StringUtils.indexOf("sdfsfsfdsf","4")); // 结果是-1
System.out.println(StringUtils.indexOf("sdfsfsfdsf","f")); // 结果是2
// 2.查找searchChar在字符串中最后一次出现的索引
System.out.println(StringUtils.lastIndexOf("aFkyk","k")); // 结果是4
System.out.println(StringUtils.lastIndexOf("aFkyk","")); // 结果是5
// 3.找出字符数组searChars第一次出现在字符串中的位置
System.out.println(StringUtils.indexOfAny("sdsfhh10","f")); // 结果是3
// 4.找出字符串中不在字符数组searchars中的第一个字符出现的位置(从0位开始)如果都在,返回-1
System.out.println(StringUtils.indexOfAnyBut("sdsfhh10","h")); // 结果是0
System.out.println(StringUtils.indexOfAnyBut("sdsfhh10","s")); // 结果是1
System.out.println(StringUtils.indexOfAnyBut("aa","aa")); // 结果是-1
// 5.统计参数1和参数2开始部分共有的字符个数
System.out.println(StringUtils.indexOfDifference("sdsfdsf","s")); // 结果是1
// 6.去掉参数2在参数1开始部分共有的字符串
System.out.println(StringUtils.difference("好好学习天天向上","好好天天")); //结果是天天