Google Guava Splitter
2017-03-28 01:20
405 查看
String.split的特殊情况
输出结果为:
String.split 自动忽略了末尾的空白内容。
不管b后面多少个逗号,返回的数组长度都是4。
改用Guava的Spliter来进行分割
输出结果为:
可以很好的分割字符串,而且语义优雅。
String[] split = ",a,,b,".split(","); for (String s : split) { System.out.println(s); } System.out.println(split.length);
输出结果为:
a b 4
String.split 自动忽略了末尾的空白内容。
不管b后面多少个逗号,返回的数组长度都是4。
改用Guava的Spliter来进行分割
public class TestSplit { public static void main(String[] args) { String data = "hello=1&world=2&k=11"; CaseInsensitiveMap map = new CaseInsensitiveMap( Splitter.on("&").trimResults().withKeyValueSeparator(Splitter.on("=").trimResults().limit(2)) .split(data)); Set<Map.Entry<String, String>> set = map.entrySet(); set.forEach(o -> System.out.println(((Map.Entry) o).getKey() + " , " + ((Map.Entry) o).getValue())); System.out.println("-------------------------------"); Iterable<String> split = Splitter.on("=").omitEmptyStrings().trimResults().split("1= 2,==2=3,=,3=4"); for (String s : split) { System.out.println(s); } } }
输出结果为:
k , 11 hello , 1 world , 2 ------------------------------- 1 2, 2 3, ,3 4
可以很好的分割字符串,而且语义优雅。
相关文章推荐
- Google Guava的splitter用法
- google的guava工具类splitter和apache stringutil对比
- Google Guava 库 CharMatcher 、Joiner以及Splitter
- Google Guava学习笔记——基础工具类Splitter的使用
- Google的Guava工具类splitter和apache stringutil对比 编辑
- [Google Guava] 1.5-Throwables:简化异常和错误的传播与检查
- Google Guava官方教程(中文版)
- Google Guava官方教程(中文版)
- Google Guava 缓存实现接口的限流
- com.google.guava工具(1)
- google guava使用介绍
- Google Guava Collections 使用介绍
- Google Guava官方教程转载
- Google Guava介绍
- Google Guava学习笔记——基础工具类针对Object类的使用
- [Google Guava] 4-函数式编程
- google guava中定义的String操作
- Google-Guava学习:用Optional<T>表示可能为null的T类型引用
- 关注google的guava工具包
- [Google Guava]学习--新集合类型BiMap