Apache Commons之commons-lang

org.apache.commons.lang3
此包主要是高度可重用静态的工具方法,主要是对java.lang类的一些补充。
package com.cxl.beanutil.test;

import org.apache.commons.lang3.StringUtils;

/**
 * 2017/9/26.
 */
public class TestString {

    public static void main(String[] args) {
        //缩短到某长度,用...结尾.其实就是(substring(str, 0, max-3) + "...")
        StringUtils.abbreviate("abcdefg", 6);//abc...

        //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
        StringUtils.appendIfMissing("abc", "xyz");//abcxyz
        StringUtils.appendIfMissingIgnoreCase("abcXYZ", "xyz");//abcXYZ

        //首字母大小写转换
        StringUtils.capitalize("welcome");//Welcome
        StringUtils.uncapitalize("Welcome");   //welcome

        //字符串扩充至指定大小且居中(若扩充大小少于原字符大小则返回原字符,若扩充大小为 负数则为0计算 )
        StringUtils.center("xin", 3); //"xin"
        StringUtils.center("xin", -1);    //"xin"
        StringUtils.center("xin", 6);    //" xin  "
        StringUtils.center("xin", 9, "yz"); //"yzyxinyzy"

        //去除字符串中的"\n", "\r", or "\r\n"
        StringUtils.chomp("abc\r\n\r\n\r\n");//"abc"

        //判断一字符串是否包含另一字符串
        StringUtils.contains("xin", "l");
        StringUtils.containsIgnoreCase("xin", "I");

        //统计一字符串在另一字符串中出现次数
        StringUtils.countMatches("xinxin", "i");//2

        //删除字符串中的梭有空格
        StringUtils.deleteWhitespace("   x  i   n   ");//"xin"


    }
}

 相关jar包

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>

 

posted @ 2017-09-26 12:18  菲儿飞飞  Views(373)  Comments(0Edit  收藏  举报