Guava 字符处理相关

1.joiner连接器

public class hellowolrd {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 23, 4, 2, 5);
        HashMap map = Maps.newHashMap();
        map.put("name1","tom1");
        map.put("name2","tom2");
        String toString = Joiner.on("=").join(list).toString();
        System.out.println(toString);//1=23=4=2=5
        StringBuilder hello_world = new StringBuilder("HELLO WORLD");
        String toString1 = Joiner.on("=").appendTo(hello_world, list).toString();//param1 :实现Appendable接口 param2 可迭代对象,数组,iterator等
        System.out.println(toString1);//HELLO WORLD1=23=4=2=5
        //useForNull("xxx")用xxx替换空值
        //skipNulls()跳过空值
        String toString2 = Joiner.on("=").withKeyValueSeparator(":").join(map).toUpperCase().toString();
        System.out.println(toString2);//NAME2:TOM2=NAME1:TOM1
    }
}

2.splitter分割器

与joiner操作对照,可按字符,定长,正则切割字符串

public class hellowolrd {
    public static void main(String[] args) throws UnsupportedEncodingException {
        Map<String, String> result7 = Splitter.on(Pattern.compile("@")).trimResults()
                .omitEmptyStrings().withKeyValueSeparator("=").split("@name=tom@ @age=67@");
    }
}//{name=tom, age=67}

3.charmatcher字符匹配器

public class hellowolrd {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //1.获取匹配器。如
        CharMatcher charMatcher = CharMatcher.inRange('i', 'o');
        //匹配器常量
        /*ANY	NONE	WHITESPACE	BREAKING_WHITESPACE
        INVISIBLE	DIGIT	JAVA_LETTER	JAVA_DIGIT
        JAVA_LETTER_OR_DIGIT	JAVA_ISO_CONTROL	JAVA_LOWER_CASE	JAVA_UPPER_CASE
        ASCII	SINGLE_WIDTH*/
        //2.操作
        String s1 = charMatcher.replaceFrom("hello", "WORLD");//hellWORLD
        //主要操作
        //collapseFrom(CharSequence,   char)	        把每组连续的匹配字符替换为特定字符。如WHITESPACE.collapseFrom(string, ‘ ‘)把字符串中的连续空白字符替换为单个空格。
       //matchesAllOf(CharSequence)	                测试是否字符序列中的所有字符都匹配。
       //removeFrom(CharSequence)	                从字符序列中移除所有匹配字符。
       //retainFrom(CharSequence)   	                在字符序列中保留匹配字符,移除其他字符。
       //trimFrom(CharSequence)	                        移除字符序列的前导匹配字符和尾部匹配字符。
       //replaceFrom(CharSequence, CharSequence)	用特定字符序列替代匹配字符。
    }
}

4.charsets字符集

public class hellowolrd {
    public static void main(String[] args) throws UnsupportedEncodingException {
        byte[] bytes = "hello world".getBytes("UTF-8");
        byte[] bytes1 = "hello world".getBytes(Charsets.UTF_8);
    }
}

5.caseformat格式化

public class hellowolrd {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //LOWER_CAMEL	        lowerCamel
        //LOWER_HYPHEN	        lower-hyphen
        //LOWER_UNDERSCORE	lower_underscore
        //UPPER_CAMEL	        UpperCamel
        //UPPER_UNDERSCORE	UPPER_UNDERSCORE
        //选择原本格式,再指定需要转换成的格式和待转换字符串
        String helloWorld = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "HelloWorld");
        System.out.println(helloWorld);//HELLO_WORLD
    }
}

6.strings字符串工具


以上方法分别为比较前缀后缀,重复字符串,空值转换,前后填充(制定字符串与长度,长度不够向前或后填充指定字符)

Excerice 将一串字母每隔两位换成*,如123456789转换成12**56**9;
public class hellowolrd {
    public static void main(String[] args) throws UnsupportedEncodingException {
         String str="123456789";
         List<String> stringList = Splitter.fixedLength(2).splitToList(str).parallelStream().collect(toList());

        Stream<HashMap<String, Integer>> zip = Streams.zip(stringList.stream(), IntStream.rangeClosed(1, stringList.size()).boxed(), (a, b) -> {
            HashMap<String, Integer> map = new HashMap<>();
            map.put(a, b);
            return map;
        });
        List<String> strings = zip.flatMap(i -> i.entrySet().stream()).filter(i -> i.getValue() % 2 != 0).map(i -> i.getKey()).collect(toList());
        String result = Joiner.on("**").join(strings).toString();//12**56**9
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值