Guava: Joiner Splitter CharMatcher CaseFormat
java guava    2018-09-01 00:04:07    0    0    0
cqc   java guava

Joiner

Joiner joiner = Joiner.on("; ").skipNulls();
return joiner.join("Harry", null, "Ron", "Hermione");
Joiner 实例是不可变对象,其每一次的配置都是返回一个新的对象,这个能保证其线程安全

Splitter

Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.split("foo,bar,, qux");
工厂方法
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)
修饰方法
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 to trimResults(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 tjjhe specified number of strings have been returned.
Splitter.on(',').limit(3).split("a,b,c,d") returns "a", "b", "c,d"
Splitter 实例是不可变对象,其每一次的配置都是返回一个新的对象,这个能保证其线程安全

CharMatcher

简单说,CharMatcher可以认为是一组字符组。
String noControl = CharMatcher.javaIsoControl().removeFrom(string); // 移除控制字符
String theDigits = CharMatcher.digit().retainFrom(string); // 仅保留数字
String spaced = CharMatcher.whitespace().trimAndCollapseFrom(string, ' '); // 将多个的空格或换行替换为一个空格, 也就是多行变一行
String noDigits = CharMatcher.javaDigit().replaceFrom(string, "*"); // 将所有数字替换为*
String lowerAndDigit = CharMatcher.javaDigit().or(CharMatcher.javaLowerCase()).retainFrom(string); // 保留数字或小写字母
内置的工厂方法
  • any()
  • none()
  • whitespace()
  • breakingWhitespace()
  • invisible()
  • digit()
  • javaLetter()
  • javaDigit()
  • javaLetterOrDigit()
  • javaIsoControl()
  • javaLowerCase()
  • javaUpperCase()
  • ascii()
  • singleWidth()
定制的CharMatcher生成方法
Method
Description
anyOf(CharSequence)
Specify all the characters you wish matched. For example, CharMatcher.anyOf("aeiou") matches lowercase English vowels.
is(char)
Specify exactly one character to match.
inRange(char, char)
Specify a range of characters to match, e.g. CharMatcher.inRange('a', 'z').
negate()
逻辑非
and(CharMatcher)
逻辑与
or(CharMatcher)
逻辑或
使用
Method
Description
collapseFrom(CharSequence, char)
Replace each group of consecutive matched characters with the specified character. For example, WHITESPACE.collapseFrom(string, ' ') collapses whitespaces down to a single space.
matchesAllOf(CharSequence)
Test if this matcher matches all characters in the sequence. For example, ASCII.matchesAllOf(string) tests if all characters in the string are ASCII.
removeFrom(CharSequence)
Removes matching characters from the sequence.
retainFrom(CharSequence)
Removes all non-matching characters from the sequence.
trimFrom(CharSequence)
Removes leading and trailing matching characters.
replaceFrom(CharSequence, CharSequence)
Replace matching characters with a given sequence.

CaseFormat

一个简便的代码风格转换工具
CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "CONSTANT_NAME")); // returns "constantName"
Format
Example
LOWER_CAMEL
lowerCamel
LOWER_HYPHEN
lower-hyphen
LOWER_UNDERSCORE
lower_underscore
UPPER_CAMEL
UpperCamel
UPPER_UNDERSCORE
UPPER_UNDERSCORE

上一篇: Guava:ListenableFuture

下一篇: Guava Primitives(原型类相关的工具类)

文档导航