Guava学习笔记之Joiner ,Strings,Splitter 工具实例
一.Strings
package com.spring_boot.guava.strings;
import com.google.common.base.Strings;
import org.junit.Test;
/**
* Created by wanggs on 2017/9/30.
* 一.Strings
*/
public class StringsTest {
@Test
public void right() {
String str = "abcd";
String rstr = Strings.padEnd(str, 10, 'x');
System.out.println(rstr);
// 结果为:abcdxxxxxx
}
@Test
public void left() {
String str = "abcd";
String rstr = Strings.padStart(str, 10, 'x');
System.out.println(rstr);
// 结果为:xxxxxxabcd
}
@Test
public void isNullOrEmpty() {
String str = "";
boolean result = Strings.isNullOrEmpty(str);
System.out.println(result);
// 结果为: true
}
@Test
public void nullToEmpty() {
//Strings.nullToEmpty 如果为null 转为""
String str = null;
String result = Strings.nullToEmpty(str);
System.out.println(result);
// 结果为:
}
@Test
public void emptyToNull() {
//Strings.emptyToNull 如果为"" 转为null
String str = "";
String result = Strings.emptyToNull(str);
System.out.println(result);
// 结果为:null
}
@Test
public void repeat() {
String str = "123";
String result = Strings.repeat(str, 3);
System.out.println(result);
// 结果为: 123123123
}
@Test
public void commonPrefix() {
//Strings.commonPrefix 获取a,b左公共部分字符串
String a = "abcdsfsfs";
String b = "abc3sfsd";
String c = Strings.commonPrefix(a, b);
System.out.println(c);
// 结果为:abc
}
@Test
public void commonSuffix() {
//Strings.commonSuffix 获取a,b右公共部分字符串
String a = "faaxyz";
String b = "fwefxyz";
String c = Strings.commonSuffix(a, b);
System.out.println(c);
// 结果为:xyz
}
}
二,Joiner 连接工具类
package com.spring_boot.guava.Joiner;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
/**
* Created by wanggs on 2017/9/30.
* 二,Joiner 连接工具类
* <p>
* 分析源码可知:该类构造方法被private修身,无法直接通过new实现
* 通过调用on传如分隔符来得到实例。
*/
public class JoinerTest {
@Test
public void list() {
// 1.连接list列表
Joiner joiner = Joiner.on(",");
List<String> list = Lists.newArrayList("a", "b", "c");
System.out.println(joiner.join(list));
// 结果为: a,b,c
}
@Test
public void iterator() {
// 2.连接Iterator<T>列表
Joiner joiner = Joiner.on(",");
List<String> list = Lists.newArrayList("a", "b", "c");
Iterator<String> iterator = list.iterator();
System.out.println(joiner.join(iterator));
// 结果为:a,b,c
}
@Test
public void strings(){
// 3.连接多个字符串
Joiner joiner = Joiner.on(",");
System.out.println(joiner.join("123","345","56","43"));
// 结果:123,345,56,43
}
@Test
public void listString(){
// 4.连接字符串与列表
Joiner joiner = Joiner.on(",");
List<String> list = Lists.newArrayList("a", "b", "c");
StringBuilder builder=new StringBuilder("标题");
System.out.println(joiner.appendTo(builder,list));
// 结果:标题a,b,c
}
@Test
public void jumpnull(){
Joiner joiner = Joiner.on(",").skipNulls();
List<String> list = Lists.newArrayList("a", null,"b", "c");
StringBuilder builder=new StringBuilder("标题");
System.out.println(joiner.appendTo(builder,list));
// 结果:标题a,b,c
}
@Test
public void replace(){
Joiner joiner = Joiner.on(",").useForNull("123");//替换null为123
List<String> list = Lists.newArrayList("a", null,"b", "c");
StringBuilder builder=new StringBuilder("标题");
System.out.println(joiner.appendTo(builder,list));
// 结果:标题a,123,b,c
}
}
三.Splitter 工具类
package com.spring_boot.guava.splitter;
import com.google.common.base.Splitter;
import com.google.common.escape.Escaper;
import com.google.common.html.HtmlEscapers;
import org.junit.Test;
import java.util.List;
/**
* Created by wanggs on 2017/9/30.
* 主要功能是拆分字符串为集合 Map等。
*/
public class SplitterTest {
@Test
public void splitterList() {
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]
}
@Test
public void ignore() {
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]
}
@Test
public void ignoreSp() {
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]
}
@Test
public void htmlEscapers() {
Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
System.out.println(htmlEscaper.escape("<a>你好</a>"));
// 结果:<a>你好</a>
}
}
网友评论