码迷,mamicode.com
首页 > 其他好文 > 详细

Guava学习笔记之Joiner ,Strings,Splitter 工具实例

时间:2014-12-08 21:32:39      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:joiner   splitter   guava   

Guava
一.Strings
1.Strings.padEnd方法
//padEnd普全右
		String a="12345";
		String b=Strings.padEnd(a, 10, 'x');
		System.out.println(b);

结果为:12345xxxxx
2.Strings.padStart方法
//padStart 普全左
		String a="12345";
		String b=Strings.padStart(a, 10, 'x');
		System.out.println(b);
		结果为:xxxxx12345



3.Strings.isNullOrEmpty 方法
//Strings.isNullOrEmpty 校验空值
		String a="";
		boolean b=Strings.isNullOrEmpty(a);
		System.out.println(b);
结果为:true

4.Strings.nullToEmpty  方法
	//Strings.nullToEmpty  如果为null 转为""
		String a=null;
		String b=Strings.nullToEmpty(a);
		System.out.println(b);
		结果为:


5.Strings.emptyToNull  方法
	//Strings.emptyToNull  如果为"" 转为null
		String a="";
		String b=Strings.emptyToNull(a);
		System.out.println(b);
		结果为:null





6.Strings.repeat  方法
	//Strings.repeat  重复字符串
		String a="123";
		String b=Strings.repeat(a, 3);
		System.out.println(b);
结果为:123123123

7.Strings.commonPrefix 方法
	//Strings.commonPrefix 获取a,b左公共部分字符串
		String a="abcdsfsfs";
		String b="abc3sfsd";
		String c=Strings.commonPrefix(a, b);
		System.out.println(c);
		结果为:abc

		
8.Strings.commonSuffix 方法
	//Strings.commonSuffix 获取a,b右公共部分字符串
		String a="faaxyz";
		String b="fwefxyz";
		String c=Strings.commonSuffix(a, b);
		System.out.println(c);
		System.out.println(c);
		结果为:xyz





















二,Joiner 连接工具类

分析源码可知:该类构造方法被private修身,无法直接通过new实现
通过调用on传如分隔符来得到实例。
1.连接List列表
Joiner joiner =  Joiner.on(",");
		List<String> list =new ArrayList<String>();
		list.add("xiaoming");
		list.add("xiaohong");
		list.add("wangsan");
		System.out.println(joiner.join(list));
结果:xiaoming,xiaohong,wangsan

2.连接Iterator<T>列表
Joiner joiner =  Joiner.on(",");
		List<String> list =new ArrayList<String>();
		list.add("xiaoming");
		list.add("xiaohong");
		list.add("wangsan");
		Iterator<String> it=list.iterator();
		System.out.println(joiner.join(it));
结果:xiaoming,xiaohong,wangsan

3.连接多个字符串 
Joiner joiner =  Joiner.on(",");  
System.out.println(joiner.join("123","345","56","43"));
结果:123,345,56,43

4.连接字符串与列表

	Joiner joiner =  Joiner.on(",");
		List<String> list =new ArrayList<String>();
		list.add("xiaoming");
		list.add("xiaohong");
		list.add("wangsan"); 
		StringBuilder builder=new StringBuilder("标题");
		System.out.println(joiner.appendTo(builder,list));
结果:标题xiaoming,xiaohong,wangsan

5.跳过null值连接
		Joiner joiner =  Joiner.on(",").skipNulls();
		List<String> list =new ArrayList<String>();
		list.add("xiaoming");
		list.add(null);
		list.add("wangsan"); 
		StringBuilder builder=new StringBuilder("标题");
		System.out.println(joiner.appendTo(builder,list));
6.替换null值进行连接
	Joiner joiner =  Joiner.on(",").useForNull("123");//替换null为123
		List<String> list =new ArrayList<String>();
		list.add("xiaoming");
		list.add(null);
		list.add("wangsan"); 
		StringBuilder builder=new StringBuilder("标题");
		System.out.println(joiner.appendTo(builder,list));
结果:标题xiaoming,123,wangsan


三.Splitter 工具类 
主要功能是拆分字符串为集合 Map等。

通过分析源码可知,该工具类同意通过 on函数传入拆分字符得到实例。

1.拆分字符串为List集合 
	Splitter splitter =Splitter.on(",");
		String str="a,b,c,d,e,f,g";
		List<String> list=splitter.splitToList(str);
		System.out.println(list);
结果:[a, b, c, d, e, f, g]
2.忽略空字符

		Splitter splitter =Splitter.on(",").omitEmptyStrings();
		String str="a,b,c,d,,f,g";
		List<String> list=splitter.splitToList(str);
		System.out.println(list);
结果:[a, b, c, d, f, g]

3.忽略空字符且去除字符串前后空格
Splitter splitter =Splitter.on(",").omitEmptyStrings().trimResults();
		String str="a,b,c,d,,f,  g  ";
		List<String> list=splitter.splitToList(str);
		System.out.println(list);

结果:[a, b, c, d, f, g]





HtmlEscapers工具类
将html转义如下源码所示

		Escaper htmlEscaper =HtmlEscapers.htmlEscaper();
		System.out.println(htmlEscaper.escape("<a>你好</a>"));
		
结果:<a>你好</a>


Guava学习笔记之Joiner ,Strings,Splitter 工具实例

标签:joiner   splitter   guava   

原文地址:http://blog.csdn.net/tianyijavaoracle/article/details/41807119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!