guava的String之Splitter

1.常用方法

摘自官网的部分常用方法说明。

Base Factories

Method Description Example
Splitter.on(char) Split on occurrences of a specific, individual character. Splitter.on(';')
Splitter.on(CharMatcher) Split on occurrences of any character in some category. Splitter.on(CharMatcher.BREAKING_WHITESPACE)
Splitter.on(CharMatcher.anyOf(";,."))
Splitter.on(String) Split on a literal String. Splitter.on(", ")
Splitter.on(Pattern)
Splitter.onPattern(String)
Split on a regular expression. Splitter.onPattern("\r?\n")
Splitter.fixedLength(int) Splits strings into substrings of the specified fixed length. The last piece can be smaller than length, but will never be empty. Splitter.fixedLength(3)

Modifiers

Method Description Example
omitEmptyStrings() Automatically omits empty strings from the result. Splitter.on(',').omitEmptyStrings().
split("a,,c,d")
 returns "a", "c", "d"
trimResults() Trims whitespace from the results; equivalent totrimResults(CharMatcher.WHITESPACE). Splitter.on(',').trimResults().split
("a, b, c, d")
 returns "a", "b", "c", "d"
trimResults(CharMatcher) Trims characters matching the specified CharMatcher from results. Splitter.on(',').trimResults
(CharMatcher.is('_')).split("_a ,_b_ ,c__")
 returns "a ", "b_ ", "c".
limit(int) Stops splitting after the specified number of strings have been returned. Splitter.on(',').limit(3).split
("a,b,c,d")
 returns "a", "b", "c,d"

2.实例

package string;

import java.util.Map;


import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;

public class SplitterUse {

	public static void useMethod() {
		//根据分隔符进行分割
		String sequence="a,,-b, c,-d";
		Iterable iterator1=Splitter.on(",").split(sequence);
		System.out.println("根据分隔符进行分割:");
		for(String str: iterator1)
			System.out.println(str);
		
		//去掉分割后空的字符串
		Iterable iterator2=Splitter.on(",").omitEmptyStrings().split(sequence);
		System.out.println("去掉分割后空的字符串:");
		for(String str: iterator2)
			System.out.println(str);
		
		//去掉分后后字符串中的空格
		Iterable iterator3=Splitter.on(",").omitEmptyStrings().trimResults().split(sequence);
		System.out.println("去掉分后后字符串中的空格:");
		for(String str: iterator3)
			System.out.println(str);
		Iterable iterator5=Splitter.on(",").omitEmptyStrings().trimResults(CharMatcher.is('-')).split(sequence);
		System.out.println("去掉分后后字符串中'-':");
		for(String str: iterator5)
			System.out.println(str);
		
		//以固定长度进行分割
		//Iterable iterator4=Splitter.fixedLength(2).split(sequence);
		Iterable iterator4=Splitter.fixedLength(2).trimResults().split(sequence);
		System.out.println("以固定长度进行分割:");
		for(String str: iterator4)
			System.out.println(str);
		
		//Splitter将处理结果处理成map类型
		Map map=Splitter.on(";").omitEmptyStrings().withKeyValueSeparator(",").split("a,c;quzer,yuanrq; , ;hello,csdn");
		System.out.println("Splitter将处理结果处理成map类型:");
		for(Map.Entry entry: map.entrySet())
			System.out.println("key="+ entry.getKey()+";value="+entry.getValue());
	}

	public static void main(String[] args) {
		useMethod();
	}

}
运行结果:
根据分隔符进行分割:
a


-b
 c
-d
去掉分割后空的字符串:
a
-b
 c
-d
去掉分后后字符串中的空格:
a
-b
c
-d
去掉分后后字符串中'-':
a
b
 c
d
以固定长度进行分割:
a,
,-
b,
c
,-
d
Splitter将处理结果处理成map类型:
key=a;value=c
key=quzer;value=yuanrq
key= ;value= 
key=hello;value=csdn


你可能感兴趣的:(guava)