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

guava的String之Splitter

时间:2014-05-09 14:04:38      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:string   guava   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.实例

运行结果:
根据分隔符进行分割:
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的String之Splitter,布布扣,bubuko.com

guava的String之Splitter

标签:string   guava   splitter   

原文地址:http://blog.csdn.net/qunzer/article/details/25376633

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