Commons学习——StringUtils学习

本文详细介绍了 Apache Commons Lang3 中的 StringUtil 类,包括字符串缩略、后缀匹配添加、字符串扩充等实用方法,并通过具体示例展示了每个方法的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运行

package langDome;

/**
 *  Java码农不识Apache,敲尽一生也枉然
 *  lang3包中StringUtil类学习
 * @author http://blog.youkuaiyun.com/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"));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值