Commons学习——StringUtils学习

来源:互联网 发布:天眼通软件下载 编辑:程序博客网 时间:2024/05/16 17:41

运行

package langDome;/** *  Java码农不识Apache,敲尽一生也枉然 *  lang3包中StringUtil类学习 * @author http://blog.csdn.net/thewaiting *  */import org.apache.commons.lang3.StringUtils;public class StringDome {    public static void main(String[] args) {        // 缩短到某长度,用...结尾,其实就是(substring(str,0,max-3)+"...")        // public static String abbreviate(String str,int maxWidth)        System.out.println("缩短到某长度用...结尾: \t" + StringUtils.abbreviate("abcdefg", 5));        // 字符串结尾的后缀是否与你要的结后缀匹配,若不匹配则添加后缀        System.out.println("若不匹配添加后缀: \t" + StringUtils.appendIfMissing("abc", "xyz"));        System.out.println("如不匹配添加后缀,忽略大小写: \t" + StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz"));        // 字符串扩充指定大小并且居中(若扩充大小少于原字符大小则返回元字符,若扩充大小为负数 则为0计算)        System.out.println("扩充小于原字符串: \t" + StringUtils.center("abcd", 2));        System.out.println("扩充负数: \t" + StringUtils.center("ab", -1));        System.out.println("扩充大于原字符串: \t" + StringUtils.center("ab", 4));        System.out.println("扩充原字符串并且拼接字符: \t" + StringUtils.center("a", 4, "yz"));        System.out.println("扩充原字符串: \t" + StringUtils.center("abc", 7, ""));        // 去除字符串中的\n \ r or \r\n        System.out.println("去除字符串中的\\n \\ r or \\r\\n:\t" + StringUtils.chomp("abc\r\n"));        // 判断一字符串是否包含另一字符串        System.out.println("判断一字符串是否包含另一字符串:\t" + StringUtils.contains("abc", "z"));        System.out.println("判断忽略大小写:\t" + StringUtils.compare("abc", "A"));        // 统计以字符串在另一个字符串中出现的字数        System.out.println("统计以字符串在另一个字符串中出现的字数:\t" + StringUtils.countMatches("abba", "a"));        // 删除一字符串的所有空格        System.out.println("删除一字符串中所有的空格:\t" + StringUtils.deleteWhitespace("     a  b c  "));        // 比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串        System.out.println("比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串:\t" + StringUtils.difference("abcde", "abxyz"));        // 检查字符串结尾后缀是否匹配        System.out.println("检查字符串结尾后缀是否匹配:\t" + StringUtils.endsWith("abcdef", "def"));        System.out.println("检查字符串结尾后缀是否匹配无视大小写:\t" + StringUtils.endsWithIgnoreCase("abcdef", "DEF"));        System.out.println(                "检查字符串结尾后缀是否匹配数组中任意一个:\t" + StringUtils.endsWithAny("abcdef", new String[] { null, "xyz", "abc" }));        // 检查字符串结尾后缀是否匹配        System.out.println("检查字符串结尾后缀是否匹配:\t" + StringUtils.startsWith("abcdef", "def"));        System.out.println("检查字符串结尾后缀是否匹配无视大小写:\t" + StringUtils.startsWithIgnoreCase("abcdef", "DEF"));        System.out.println(                "检查字符串结尾后缀是否匹配数组中任意一个:\t" + StringUtils.startsWithAny("abcdef", new String[] { null, "xyz", "abc" }));        // 检查起始字符串是否相同        System.out.println("检查起始字符串是否相同:\t" + StringUtils.equals("abc", "abc"));        // 比较字符串数组内的所有元素的字符序列,起始一直则返回一致的字符串,若无则返回""        System.out.println("字符数组开头是否有相同的字符串:\t" + StringUtils.getCommonPrefix(new String[] { "abce", "abxyz" }));        // 正向查找字符串第一次出现的位置        System.out.println("正向查找字符串第一次出现的位置:\t" + StringUtils.indexOf("aaaabbb", "b"));        System.out.println("正向查找字符串第一次出现的位置从角标3后开始找:\t" + StringUtils.indexOf("aaaabbb", "b", 3));        System.out.println("正向查找字符串第2次出现的位置:\t" + StringUtils.ordinalIndexOf("aaaabbb", "b", 3));        // 反向查找字符串第一次出现的位置        System.out.println("反向查找字符串第一次出现的位置:\t" + StringUtils.lastIndexOf("aabbbbcbb", "b"));        System.out.println("反向查找字符串第一次出现的位置从角标3:\t" + StringUtils.lastIndexOf("aabbbbcbb", "b", 3));        System.out.println("反向查找字符串第n次出现的位置:\t" + StringUtils.lastOrdinalIndexOf("aabbbbcbb", "b", 2));        // 判断字符串大写还是小写        System.out.println("判断字符串大写还是小写:\t" + StringUtils.isAllUpperCase("ABC"));        System.out.println("判断字符串大写还是小写:\t" + StringUtils.isAllLowerCase("abc"));        // 判断是否为空(注:isBlank与isEmpiy 区别)        System.out.println("判断是否为空:\t" + StringUtils.isBlank(null) + "\t" + StringUtils.isBlank("  "));        System.out.println("判断是否为空:\t" + StringUtils.isNoneBlank(" ", "bar"));        System.out.println("判断是否为空:\t" + StringUtils.isEmpty(null) + "\t" + StringUtils.isEmpty(""));        System.out.println("判断是否为空:\t" + StringUtils.isEmpty(" "));        System.out.println("判断是否为空:\t" + StringUtils.isNoneEmpty(" ", "bar"));        // 判断字符串数字        System.out.println("判断字符串数字:\t" + StringUtils.isNumeric("123"));        System.out.println("判断字符串数字:\t" + StringUtils.isNumeric("12 3"));        System.out.println("判断字符串数字:\t" + StringUtils.isNumericSpace("12 3"));        // 连接所提供的数组的元素为一个字符串        System.out.println("数组中加入分割符号:\t" + StringUtils.join("a", "b", "c"));        // 大小写转化        System.out.println("大小写转换:\t" + StringUtils.upperCase("aBc"));        System.out.println("大小写转换:\t" + StringUtils.lowerCase("aBc"));        System.out.println("大小写转化(互换):\t" + StringUtils.swapCase("The Dog"));        // 替换字符串内容...(replacePattern,replceOnce)        System.out.println("单个替换:\t" + StringUtils.replace("abc", "a", "z"));        System.out.println("指定区域替换:\t" + StringUtils.overlay("abcdef", "zz", 2, 4));        System.out.println(                "多组指定替换:\t" + StringUtils.replaceEach("abcdef", new String[] { "ab", "c" }, new String[] { "w", "t" }));        // 重复字符        System.out.println("重复字符:\t" + StringUtils.repeat('e', 3));        // 反转字符        System.out.println("反转字符:\t" + StringUtils.reverse("bat"));        // 删除字符串        System.out.println("删除字符串:\t" + StringUtils.remove("queued", 'u'));        // 分割字符串        System.out.println("分割字符串:\t" + StringUtils.split("a..b.c", '.'));        System.out.println("分割字符串分为2组:\t" + StringUtils.split("ab:cd:ef", ":", 2));        System.out.println("分割字符串分为2组:\t" + StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2));        System.out.println("分割字符串保存这个分割符:\t" + StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":"));        // 去除首位空格类似与trim        System.out.println("去除首位空格类似与trim:\t" + StringUtils.strip(" ab c "));        System.out.println("去除首位空格类似与trim:\t" + StringUtils.stripToNull(null));        System.out.println("去除首位空格类似与trim:\t" + StringUtils.stripToEmpty(null));        // 截取字符串        System.out.println("截取字符串:\t" + StringUtils.substring("abce", 2));        System.out.println("截取字符串:\t" + StringUtils.substring("abce", 2, 3));        // left,right从左/右截取字符串        System.out.println("left,right从左/右截取字符串:\t" + StringUtils.left("abcd", 2));        System.out.println("left,right从左/右截取字符串:\t" + StringUtils.right("abcd", 2));        // 从第n位开始截取m字符        System.out.println("从第n位开始截取m字符:\t" + StringUtils.mid("abcdef", 2, 4));        System.out.println("从某字符前截取:\t" + StringUtils.substringBeforeLast("abcdef", "b"));        System.out.println("从某字符后截取:\t" + StringUtils.substringAfter("abcdef", "b"));        System.out.println("从某最后一个字符后截取:\t" + StringUtils.substringAfterLast("abcdef", "b"));        System.out.println("从某字符中间截取:\t" + StringUtils.substringBetween("aba", "a"));        System.out.println("从某两个字符中间截取:\t" + StringUtils.substringBetween("yabczyabcz", "y", "z"));    }}
原创粉丝点击