1. Joiner
用分隔符将一系列字符串连接在一起可能不必要地很棘手——但不应该如此。如果序列包含空值,则可能会更加困难。流畅的细木工风格使它变得简单。
Joiner joiner = Joiner.on("; ").skipNulls();
return joiner.join("Harry", null, "Ron", "Hermione");
Joiner.on(",").join(Arrays.asList(1, 5, 7)); // returns "1,5,7"
警告:joiner实例总是不可变的。joiner配置方法将始终返回一个新的joiner,您必须使用它来获得所需的语义。这使得任何Joiner线程都是安全的,并可用作静态final常量。
2. Splitter
用于分割字符串的内置Java实用程序有一些古怪的行为。例如,String.split会无提示地丢弃尾部分隔符,而StringTokenizer只考虑五个空格字符而不考虑其他字符。
Splitter允许使用一个简单明了的流畅模式完全控制所有这些令人困惑的行为
Iterable<String> iterable = Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.split("foo,bar,, qux");
Iterator<String> iterator = iterable.iterator();
while (iterator.hasNext()){
System.out.println("字符串:" + iterator.next());
} //返回"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 the specified number of strings have been returned. |
Splitter.on(',').limit(3).split("a,b,c,d") returns "a", "b", "c,d"
|
警告:拆分器实例总是不可变的。拆分器配置方法将始终返回一个新的拆分器,您必须使用它来获得所需的语义。这使得任何拆分器线程都是安全的,并可用作静态最终常量。
3. Strings
Strings类中存在数量有限的通用字符串实用工具方法
CaseFormat
CaseFormat是一个方便的小类,用于在ASCII大小写约定(例如,编程语言的命名约定)之间进行转换。支持的格式包括:
格式 | 例子 |
---|---|
LOWER_CAMEL |
lowerCamel |
LOWER_HYPHEN |
lower-hyphen |
LOWER_UNDERSCORE |
lower_underscore |
UPPER_CAMEL |
UpperCamel |
UPPER_UNDERSCORE |
UPPER_UNDERSCORE |
CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "CONSTANT_NAME") //打印结果:constantName
CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "CONSTANT_NAME") //打印结果:ConstantName
CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "constant_name") //打印结果:ConstantName
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "constant_name") //打印结果:ConstantName
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, "constantName") //打印结果:ConstantName
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "constantName") //打印结果:CONSTANT_NAME
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "ConstantName") //打印结果:CONSTANT_NAME
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "ConstantName") //打印结果:constant_name
4. Charsets
//不要这样做:
try {
bytes = string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// how can this possibly happen?
throw new AssertionError(e);
}
//而应该这样做:
bytes = string.getBytes(Charsets.UTF_8);
字符集提供对保证所有Java平台实现都支持的六个标准字符集实现的常量引用。使用它们,而不是按名称引用字符集。
5. CharMatcher
待定
参考:https://github.com/google/guava/wiki/StringsExplained#joiner
网友评论