//预设了一些字符串常量,可以直接用
private static final int STRING_BUILDER_SIZE = 256;
public static final String SPACE = " ";
public static final String EMPTY = "";
public static final String LF = "\n";
public static final String CR = "\r";
//判断字符串中是否包含某个字符串(方法比较多不一一列举,自行探索)
StringUtils.contains("abcdefghijklmn", "ABC"); // false
StringUtils.containsIgnoreCase("abcdefghijklmn", "ABC"); // true
//统计一个字符串中,某个字符串出现的次数
StringUtils.countMatches("abcdefga", "a"); // 2
//删除字符串中的梭有空格
StringUtils.deleteWhitespace(" ab c "); // "abc"
//比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
StringUtils.difference("abcde", "abxyz"); // "xyz"
//检查起始字符串是否匹配
StringUtils.startsWith("abcdef", "abc"); // true
StringUtils.startsWithIgnoreCase("ABCDEF", "abc"); // true
StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});// true
//检查字符串结尾后缀是否匹配
StringUtils.endsWith("abcdef", "def"); // true
StringUtils.endsWithIgnoreCase("ABCDEF", "def"); // true
StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});// true
//判断两字符串是否相同
StringUtils.equals("abc", "abc"); // true
StringUtils.equalsIgnoreCase("abc", "ABC"); // true
//比较字符串数组内的所有元素的字符序列,起始一致则返回一致的字符串,若无则返回""
StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"});// "ab"
//正向查找字符在字符串中第一次出现的位置
StringUtils.indexOf("aabaabaa", "b"); // 2
StringUtils.indexOf("aabaabaa", "b", 3); // 5(从角标3后查找)
StringUtils.ordinalIndexOf("aabaabaa", "a", 3); // 1(查找第n次出现的位置)
//反向查找字符串第一次出现的位置
StringUtils.lastIndexOf("aabaabaa", 'b'); // 5
StringUtils.lastIndexOf("aabaabaa", 'b', 4); // 2
StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2); // 1
//判断字符串大写、小写
StringUtils.isAllUpperCase("ABC"); // true
StringUtils.isAllLowerCase("abC"); // false
//判断是否为空(注:isBlank与isEmpty 区别)
StringUtils.isBlank(null); // true
StringUtils.isBlank(""); // true
StringUtils.isBlank(" "); // true
StringUtils.isNoneBlank(" ", "bar"); // false
StringUtils.isEmpty(null);
StringUtils.isEmpty(""); // true
StringUtils.isEmpty(" "); // false
StringUtils.isNoneEmpty(" ", "bar"); // true
//判断字符串数字
StringUtils.isNumeric("123"); // true
StringUtils.isNumeric("12 3"); // false (不识别运算符号、小数点、空格……)
StringUtils.isNumericSpace("12 3"); // true
//数组中加入分隔符号
StringUtils.join([1, 2, 3], ‘;‘); // "1;2;3"
//大小写转换
StringUtils.upperCase("aBc"); // "ABC"
StringUtils.lowerCase("aBc"); // "abc"
StringUtils.swapCase("The dog has a BONE");// "tHE DOG HAS A bone"
//替换字符串内容……(replacePattern、replceOnce)
StringUtils.replace("aba", "a", "z"); // "zbz"
StringUtils.overlay("abcdef", "zz", 2, 4); // "abzzef"(指定区域)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
new String[]{"w", "t"}); // "wcte"(多组指定替换ab->w,d->t)
//重复字符
StringUtils.repeat(‘e‘, 3); // "eee"
//反转字符串
StringUtils.reverse("bat"); // "tab"
//删除某字符
StringUtils.remove("queued", ‘u‘); // "qeed"
//分割字符串
StringUtils.split("a..b.c", ‘.‘); // ["a", "b", "c"]
StringUtils.split("ab:cd:ef", ":", 2); // ["ab", "cd:ef"]
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2);// ["ab", "cd-!-ef"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":");//-["ab"," ","cd","ef"]
//去除首尾空格,类似trim……(stripStart、stripEnd、stripAll、stripAccents)
StringUtils.strip(" ab c "); // "ab c"
StringUtils.stripToNull(null); // null
StringUtils.stripToEmpty(null); // ""
//截取字符串
StringUtils.substring("abcd", 2); // "cd"
StringUtils.substring("abcdef", 2, 4); // "cd"
//left、right从左(右)开始截取n位字符
StringUtils.left("abc", 2); // "ab"
StringUtils.right("abc", 2); // "bc"
//从第n位开始截取m位字符 n m
StringUtils.mid("abcdefg", 2, 4); // "cdef"
lang3包中常用的字符串处理工具
最新推荐文章于 2023-06-02 08:45:14 发布