`
kanpiaoxue
  • 浏览: 1781094 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Guava中Strings的操作说明(Guava 23)

 
阅读更多

 

参考范例:

String theDigits = CharMatcher.digit().retainFrom("42d3d43gfsdg4fafdasfdasf3f42fsf4fsd3"); // 42343434243
System.out.println(theDigits);
String rs = CharMatcher.whitespace().collapseFrom("42d3 d43  gfsdg4f afdasf        dasf3f 42fsf 4fsd3",' '); // 42d3 d43 gfsdg4f afdasf dasf3f 42fsf 4fsd3
System.out.println(rs);

System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "hello_world")); // helloWorld
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "_hello_world")); // HelloWorld
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "helloWorld")); // hello-world
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "helloWorld")); // hello_world
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, "helloWorld")); // HelloWorld
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "helloWorld")); // HELLO_WORLD

 

文档参考地址: https://github.com/google/guava/wiki/StringsExplained 

 

Joiner

Joining together a sequence of strings with a separator can be unnecessarily tricky -- but it shouldn't be. If your sequence contains nulls, it can be even harder. The fluent style of Joiner makes it simple.

Joiner joiner = Joiner.on("; ").skipNulls();
return joiner.join("Harry", null, "Ron", "Hermione");

returns the string "Harry; Ron; Hermione". Alternately, instead of using skipNulls, you may specify a string to use instead of null with useForNull(String).

You may also use Joiner on objects, which will be converted using their toString() and then joined.

Joiner.on(",").join(Arrays.asList(1, 5, 7)); // returns "1,5,7"

Warning: joiner instances are always immutable. The joiner configuration methods will always return a new Joiner, which you must use to get the desired semantics. This makes any Joiner thread safe, and usable as a static final constant.

Splitter

The built in Java utilities for splitting strings can have some quirky behaviors. For example, String.split silently discards trailing separators, and StringTokenizer respects exactly five whitespace characters and nothing else.

Quiz: What does ",a,,b,".split(",") return?

  1. "", "a", "", "b", ""
  2. null, "a", null, "b", null
  3. "a", null, "b"
  4. "a", "b"
  5. None of the above

The correct answer is none of the above: "", "a", "", "b". Only trailing empty strings are skipped. What is this I don't even.

Splitter allows complete control over all this confusing behavior using a reassuringly straightforward fluent pattern.

Splitter.on(',')
    .trimResults()
    .omitEmptyStrings()
    .split("foo,bar,,   qux");

returns an Iterable<String> containing "foo", "bar", "qux". A Splitter may be set to split on any PatterncharString, or CharMatcher.

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 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"

TODO: Map splitters

If you wish to get a List, just use Lists.newArrayList(splitter.split(string)) or the like.

Warning: splitter instances are always immutable. The splitter configuration methods will always return a new Splitter, which you must use to get the desired semantics. This makes any Splitter thread safe, and usable as a static final constant.

CharMatcher

In olden times, our StringUtil class grew unchecked, and had many methods like these:

  • allAscii
  • collapse
  • collapseControlChars
  • collapseWhitespace
  • lastIndexNotOf
  • numSharedChars
  • removeChars
  • removeCrLf
  • retainAllChars
  • strip
  • stripAndCollapse
  • stripNonDigits

They represent a partial cross product of two notions:

  1. what constitutes a "matching" character?
  2. what to do with those "matching" characters?

To simplify this morass, we developed CharMatcher.

Intuitively, you can think of a CharMatcher as representing a particular class of characters, like digits or whitespace. Practically speaking, a CharMatcher is just a boolean predicate on characters -- indeed, CharMatcher implements Predicate<Character> -- but because it is so common to refer to "all whitespace characters" or "all lowercase letters," Guava provides this specialized syntax and API for characters.

But the utility of a CharMatcher is in the operations it lets you perform on occurrences of the specified class of characters: trimming, collapsing, removing, retaining, and much more. An object of type CharMatcher represents notion 1: what constitutes a matching character? It then provides many operations answering notion 2: what to do with those matching characters? The result is that API complexity increases linearly for quadratically increasing flexibility and power. Yay!

String noControl = CharMatcher.javaIsoControl().removeFrom(string); // remove control characters
String theDigits = CharMatcher.digit().retainFrom(string); // only the digits
String spaced = CharMatcher.whitespace().trimAndCollapseFrom(string, ' ');
  // trim whitespace at ends, and replace/collapse whitespace into single spaces
String noDigits = CharMatcher.javaDigit().replaceFrom(string, "*"); // star out all digits
String lowerAndDigit = CharMatcher.javaDigit().or(CharMatcher.javaLowerCase()).retainFrom(string);
  // eliminate all characters that aren't digits or lowercase

Note: CharMatcher deals only with char values; it does not understand supplementary Unicode code points in the range 0x10000 to 0x10FFFF. Such logical characters are encoded into a String using surrogate pairs, and a CharMatcher treats these just as two separate characters.

Obtaining CharMatchers

Many needs can be satisfied by the provided CharMatcher factory methods:

Other common ways to obtain a CharMatcher include:

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').

Additionally, CharMatcher has negate()and(CharMatcher), and or(CharMatcher). These provide simple boolean operations on CharMatcher.

Using CharMatchers

CharMatcher provides a wide variety of methods to operate on occurrences of the specified characters in any CharSequence. There are more methods provided than we can list here, but some of the most commonly used are:

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.

(Note: all of these methods return a String, except for matchesAllOf, which returns a boolean.)

Charsets

Don't do this:

try {
  bytes = string.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
  // how can this possibly happen?
  throw new AssertionError(e);
}

Do this instead:

bytes = string.getBytes(Charsets.UTF_8);

Charsets provides constant references to the six standard Charset implementations guaranteed to be supported by all Java platform implementations. Use them instead of referring to charsets by their names.

TODO: an explanation of charsets and when to use them

(Note: If you're using JDK7, you should use the constants in StandardCharsets

CaseFormat

CaseFormat is a handy little class for converting between ASCII case conventions — like, for example, naming conventions for programming languages. Supported formats include:

Format Example
LOWER_CAMEL lowerCamel
LOWER_HYPHEN lower-hyphen
LOWER_UNDERSCORE lower_underscore
UPPER_CAMEL UpperCamel
UPPER_UNDERSCORE UPPER_UNDERSCORE

Using it is relatively straightforward:

CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "CONSTANT_NAME")); // returns "constantName"

We find this especially useful, for example, when writing programs that generate other programs.

 

 

分享到:
评论

相关推荐

    google guava 中文教程

    Guava提供了更高效、更简洁的字符串处理工具,如Strings类中的join()方法用于拼接字符串,Joiner类提供了更灵活的连接功能,Splitter类则帮助我们根据特定分隔符拆分字符串。 五、函数式编程 Guava引入了Function...

    guava 常用API说明

    Guava的Strings类提供了多种处理字符串的方法,如join、split、leftPad、rightPad等,使得字符串操作更加方便。 6. **预定义常量与检查类** Guava提供了Preconditions、Check类,它们包含了一系列静态方法用于...

    guava-23.0.zip guava.jar guava

    6. **IO流操作**:Guava的Files、ByteStreams、CharStreams等类提供了对文件和流的操作,简化了文件读写和流转换。 7. **检查类**:Preconditions、Objects等类提供了参数检查和对象验证功能,有助于防止程序中的...

    guava-18.0(guava-18.0.jar和guava-18.0-sources.jar)

    5. **字符串处理**:Guava提供了Strings类,包含各种字符串操作的静态方法,如Escaper和Joiner等工具。 6. **I/O工具**:Guava提供了Files、ByteStreams、CharStreams和Charsets等类,简化了文件和流的操作。 7. **...

    google开源项目guava.jar包

    字符串处理在Guava中也得到了强化,如Strings类提供了丰富的静态方法,可以方便地进行字符串的清理、比较、格式化等操作。另外,Splitter和Joiner类则帮助我们更灵活地处理字符串分割与连接。 I/O操作在Guava中通过...

    Google中的Guava源码

    Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, ...

    guava14.0.1jar包

    5. **字符串处理**:Guava的Strings类包含了许多实用的字符串操作方法,如split、join、substringAfter等,简化了对字符串的操作。 6. **I/O操作**:Guava的IO类提供了对文件、输入/输出流的高效处理,包括读写操作...

    guava-18.0资料

    5. **字符串处理**:Guava提供了Strings类,包含大量用于字符串操作的方法,如Joiner和Splitter,简化了字符串连接和分割的操作。 6. **I/O操作**:Guava的Files类提供了对文件系统的低级别访问,而CharStreams和...

    Guava常用类库 v33.0.0.zip

    6. **I/O工具**:Guava的Files、ByteStreams和CharStreams类提供了许多方便的静态方法,用于处理文件、字节流和字符流操作,简化了I/O操作。 7. **原生类型支持**:Guava提供了对基本类型(如int、char等)的泛型...

    guava-r09-jarjar.jar

    5. **字符串处理**:Guava的Strings类提供了许多便捷的字符串操作,如joining、blankness检查、CaseFormat转换等。 6. **原生类型支持**:Guava对int、long、char等原生类型提供了泛型安全的支持,避免了装箱和拆箱...

    不加密Google Guava视频教程.txt

    ├─Google Guava 第25讲-Guava之RateLimiter在漏桶限流算法中的使用.wmv ├─Google Guava 第26讲-Guava之RateLimiter令牌桶算法的使用.wmv ├─Google Guava 第27讲-ListenableFuture,FutureCallBack讲解.wmv ...

    guava-26.0-jre.zip

    5. **字符串处理**:Guava提供了Strings类,包含了一系列用于操作和检查字符串的方法,如join、split、isBlank等,简化了字符串处理的复杂性。 6. **流API**:在Java 8之前,Guava就引入了Stream API的概念,虽然与...

    Guava Source Code 22.0

    Guava的Strings类提供了大量静态方法,用于字符串的格式化、比较、操作等。此外,Files、CharStreams和ByteStreams等类为文件和流操作提供了便利,例如,CharSource和ByteSink抽象了读写操作,使得操作更加简洁。 ...

    guava(google的java集合包)

    10. **枚举集**:Guava的EnumSet和EnumMap优化了对枚举类型的集合操作,相比Java标准库中的HashSet和HashMap,它们在性能上有显著优势。 Guava的这些特性使其成为许多Java项目中的首选库,它不仅丰富了Java的工具箱...

    guava-18.0.jar

    同时,Guava的Strings类提供了一系列字符串处理方法,如Joiner和Splitter,使得字符串操作更为便捷。 总的来说,Guava库通过其丰富的功能和强大的工具,极大地提高了Java开发的效率和代码质量。在解决RocketMQ过滤...

    guava-r07.zip

    Guava是Google开发的一个Java库,它包含许多Google的核心库,如集合、缓存、并发库、原生类型支持、字符串...如果你在项目中遇到了缺少Guava R07 jar包的情况,下载并引入它将能解决许多问题,并带来更好的编程体验。

    Google的guava实战

    Guava的字符串处理工具包括了对字符串的空值检查、分割、截取、匹配等常见操作的封装,如Strings工具类。使用Guava的字符串处理工具可以避免编写冗长的字符串操作代码,同时使代码更加易于阅读和维护。 在I/O操作...

    Google-guava 19.0

    Guava的Strings类提供了大量用于处理和检查字符串的方法,如contains、startsWith、endsWith、join等,极大地提高了字符串操作的便利性。此外,Splitter和Joiner类则在字符串分割和合并上提供了更高效的方式。 **6....

    guava-18.0.rar

    5. **字符串处理**:Guava的Strings类提供了许多有用的静态方法,如isNullOrEmpty、substringBefore、substringAfter等,简化了字符串操作。 6. **I/O操作**:Guava的Files、Paths和ByteStreams等类提供了更方便的...

    guava23.0,用于安卓软件开发

    4. **字符串处理**:Guava提供了Strings类,包含了诸如blank、isEmpty、isNullOrEmpty等实用方法,使得字符串处理更加简便。 5. **函数式编程**:Guava引入了Function、Predicate等接口,支持函数式编程风格,这在...

Global site tag (gtag.js) - Google Analytics