StringUtils工具类常用方法
org.apache.commons.lang.StringUtils
package cn.qhj.test;
import org.apache.commons.lang.StringUtils;
/**
* @author QHJ
* @date 2021/11/6 10:10
* @description:
*/
public class stringutils {
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = "";
// 1、判断字符串是否为(非)空
System.out.println(StringUtils.isEmpty(str1)); //false
System.out.println(StringUtils.isEmpty(str2)); //true
System.out.println(StringUtils.isNotEmpty(str2));//false
// 2、判断字符串是否为空
System.out.println(StringUtils.isBlank(str1)); //false
System.out.println(StringUtils.isBlank(str2)); //true
System.out.println(StringUtils.isNotBlank(str2));//false
// 3、去除字符串两端的控制符、空字符串(null返回null)
System.out.println(StringUtils.trim(" 123abc+")); //123abc+
System.out.println(StringUtils.trim("")); //
System.out.println(StringUtils.trim(null)); //null
// 4、去除字符串两端的控制符、空字符串(null返回"")
System.out.println(StringUtils.trimToEmpty(" 123abc+")); //123abc+
System.out.println(StringUtils.trimToEmpty("")); //
System.out.println(StringUtils.trimToEmpty(null)); //
// 5、去除字符串两端的空白符、空字符串(null返回null)
System.out.println(StringUtils.stripToNull(" abc ")); //abc
System.out.println(StringUtils.stripToNull("")); //null
System.out.println(StringUtils.stripToNull(null)); //null
// 6、去除字符串两端的空白符、空字符串(null返回"")
System.out.println(StringUtils.stripToEmpty(" abc ")); //abc
System.out.println(StringUtils.stripToEmpty("")); //
System.out.println(StringUtils.stripToEmpty(null)); //
// 1、字符串以prefix开始
System.out.println(StringUtils.startsWith("abcdef123456", "")); //true
System.out.println(StringUtils.startsWith("abcdef123456", "abc")); //true
System.out.println(StringUtils.startsWith("abcdef123456", "bc")); //false
System.out.println(StringUtils.startsWith("abcdef123456", "123")); //false
// 2、字符串以prefix开始,不区分大小写
System.out.println(StringUtils.startsWithIgnoreCase("abcdef123456", "Abcd")); //true
// 3、字符串以数组中的字符串开始
System.out.println(StringUtils.startsWithAny("abcdef123456", new String[]{"g", "abc"})); //true
System.out.println(StringUtils.startsWithAny("abcdef123456", new String[]{"a", "abc"})); //true
// 4、字符串以suffix结束
System.out.println(StringUtils.endsWith("abcdef123456", "456")); //true
System.out.println(StringUtils.endsWith("abcdef123456", "6")); //true
System.out.println(StringUtils.endsWith("abcdef123456", "3")); //false
// 5、替换字符串:把str中的abc替换成cba,max是最大替换次数,默认是替换所有
System.out.println(StringUtils.replaceOnce("abcdef123456", "abc", "cba")); //cbadef123456
System.out.println(StringUtils.replace("cbadef123456", "cba", "abc", 2)); //max:最大替换次数
System.out.println(StringUtils.replaceChars("abc123abcabc", "abc", "y")); //替换所有字符,将所有的abc替换成y,结果是:y123yy
// 6、按照数组批量指定要替换的字符串,位置要匹配,数组长度要相等
System.out.println(StringUtils.replaceEach("www.baidu.com", new String[]{"baidu", "com"}, new String[]{"taobao", "net"}));
System.out.println(StringUtils.replaceEach("www.baidu,baidu.com", new String[]{"baidu", "com"}, new String[]{"taobao", "net"}));
System.out.println(StringUtils.replaceEachRepeatedly("www.baidu.com", new String[]{"baidu", "com"}, new String[]{"taobao", "net"}));
// 7、比较两个字符串是否相等,如果两个均为null,则也认为相等
System.out.println(StringUtils.equals("", "")); //true
System.out.println(StringUtils.equals(null, null)); //true
System.out.println(StringUtils.equals("", null)); //false
System.out.println(StringUtils.equals(null, "")); //false
System.out.println(StringUtils.equalsIgnoreCase("ss", "Ss")); //true
// 8、返回searchChar在字符串中第一次出现的位置,如果searchChar没有在字符串中出现,则返回-1
System.out.println(StringUtils.indexOf("abcdef123456", "4")); //9
System.out.println(StringUtils.indexOf("abcdef123456", "f")); //5
// 查找searchChar在字符串中最后一次出现的索引
System.out.println(StringUtils.lastIndexOf("abcdef123456a", "a")); //12
System.out.println(StringUtils.lastIndexOf("abcdef123456a", "")); //13
// 9、找出字符数组searChars第一次出现在字符串中的位置
System.out.println(StringUtils.indexOfAny("abcabc123456a", "a")); //0
System.out.println(StringUtils.indexOfAny("abcabc123456ab", new String[]{"a", "b"})); //0
System.out.println(StringUtils.indexOfAny("abcabc123456abc", new String[]{"c", "b"})); //1
System.out.println(StringUtils.indexOfAny("abcabc123456abc", new String[]{"a", "c", "1"})); //0
System.out.println(StringUtils.lastIndexOfAny("abcabc123456", new String[]{"abc", "bc"})); //4 找出bc最后一次出现的位置
// 10、找出字符串中不在字符数组searchars中的第一个字符出现的位置(从0位开始)——如果都在,返回-1
System.out.println(StringUtils.indexOfAnyBut("abcabc123456", "c")); //0
System.out.println(StringUtils.indexOfAnyBut("abcabc123456abc", "a")); //1
System.out.println(StringUtils.indexOfAnyBut("aa", "aa")); //-1
// 11、统计参数1和参数2开始部分共有的字符个数
System.out.println(StringUtils.indexOfDifference("abcabc123456abc", "abc")); //3
System.out.println(StringUtils.indexOfDifference(new String[]{"abcabc12345abc", "a"})); //1
// 12、去掉参数2在参数1开始部分共有的字符串
System.out.println(StringUtils.difference("啊啊啊啊啊", "啊啊啊啊啊嗷嗷")); //嗷嗷
// 13、查找,不区分大小写,没有找到返回-1
System.out.println(StringUtils.indexOfIgnoreCase("abcabcAbc123", "abc")); //0
System.out.println(StringUtils.indexOfIgnoreCase("AbcAbC123", "ab", 2)); // 3 从指定位置开始查找,不区分大小写--返回-1
System.out.println(StringUtils.indexOfIgnoreCase("ABCabc1234abc", "abc", 1)); //3
System.out.println(StringUtils.lastIndexOfIgnoreCase("", "")); //0
System.out.println(StringUtils.lastIndexOfIgnoreCase("", "", 2)); //0
// 14、截取指定位置的字符串
System.out.println(StringUtils.substring("abcabc123456abc", 3)); //abc123456abc
System.out.println(StringUtils.substring("abcabc123456abc", 3, 5)); //ab(左闭右开)
// 15、截取指定字符串之前的内容
System.out.println(StringUtils.substringBefore("abcabc123456abc", "bc")); //a
System.out.println(StringUtils.substringBeforeLast("abcabc123456abc", "c")); //abcabc123456ab 一直找到最后一个指定的字符串
System.out.println(StringUtils.substringAfter("abcabc123456abc", "123")); //456abc 找指定字符串之后的
System.out.println(StringUtils.substringAfterLast("abcabc123456abc", " ")); //无输出
// 16、截取参数2和参数3中间的字符
System.out.println(StringUtils.substringBetween("abcabc123456abc", "bc")); //a
System.out.println(StringUtils.substringBetween("abcabc123456abc", "bc", "bc")); //a
System.out.println(StringUtils.substringsBetween("abc123456", "a", "c")); //b 以数组方式返回两个参数之间的字符串
// 1、分割字符串,可以设定得到数组的长度,但一般情况下不要设定,这样会发生冲突
StringUtils.split("y5y,4454,545"); //默认是按,来分割
StringUtils.split("aaaa#sss","#"); //[aaaa,sss]
StringUtils.split("aaaa#sss#","#",2);
// 2、按不同类型进行分割字符串*/
StringUtils.splitByCharacterType("aa3444张三Bcss"); //[aa,3444,张三,a,B,css,B]
StringUtils.splitByCharacterTypeCamelCase(""); //[aa,3444,张三,a,Bcss,B]
// 3、分割字符串,""不会被忽略,可以设置分割字符串的数组长度
StringUtils.splitByWholeSeparator("aaaa#sss#","#"); //[aaaa,sss,] ""不会被忽略
StringUtils.splitByWholeSeparator("aaaa#sss#ggg","#"); //[aaaa,sss,ggg]
StringUtils.splitByWholeSeparator("aaaa#sss#ggg",""); //[aaaa#,sss#ggg] 按空格分割
StringUtils.splitByWholeSeparator("aaaa#sss#","#",2); //[aaaa,sss#] 2设定返回数组的最大长度/
// 4、分割字符串,""不会被忽略,可以设置分割字符串的数组长度
StringUtils.splitByWholeSeparatorPreserveAllTokens("sddssfsfasfsaf",null); //[sddssf,sfasfsaf,]
StringUtils.splitByWholeSeparatorPreserveAllTokens("sddssfsfasfsaf",""); //[sddssf,sfasfsaf,]
// 5、同上
StringUtils.splitPreserveAllTokens("");
StringUtils.splitPreserveAllTokens("","");
StringUtils.splitPreserveAllTokens("","",3);
}
}