It uses internally the String.split(String) implementation of java
and provides a guava like wrapper. This results in an implicit splitting of
the whole String compared to the lazy / deferred splitting of guava. It
focuses and RegEx-based splitting and omits the fixedLength and Map based
variants.
Migrating from Guava
In order to migrate for most case you only need to replace the package name
on the import. A major different is that the split method provided is
splitToList(String), the variant split() is replaced by it
completely.
Changes to Guavas-Splitter
It is quite similar to the guava variant but behaves a little different in
certain details, especially in the context of limit(int) and
trimResults(), omitEmptyStrings(). While
guavas version will filter the limit elements after the application of omit /
trim, this version will do it the other way round, resulting in a different
result compared to the guava version.
- Author:
- Oliver Wolff
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionUsually the separator will be pre-processed before being passed toString.split(String).limit(int maxItems) Returns a splitter that behaves equivalently tothissplitter but stops splitting after it reaches the limit.Returns a splitter that behaves equivalently tothissplitter, but automatically omits empty strings from the results.static Splitteron(char separator) Returns a splitter that uses the given fixed string as a separator.static SplitterReturns a splitter that uses the given fixed string as a separator.splitToList(String sequence) Splitssequenceinto string components and returns them as an immutable list.Returns a splitter that behaves equivalently tothissplitter, but automatically removes leading and trailing whitespace from each returned substring.
-
Constructor Details
-
Splitter
public Splitter()
-
-
Method Details
-
on
Returns a splitter that uses the given fixed string as a separator. For example,Splitter.on(", ").split("foo, bar,baz")returns an iterable containing["foo", "bar,baz"].- Parameters:
separator- the literal, nonempty string to recognize as a separator- Returns:
- a splitter, with default settings, that recognizes that separator
-
on
Returns a splitter that uses the given fixed string as a separator. For example,Splitter.on(", ").split("foo, bar,baz")returns an iterable containing["foo", "bar,baz"].- Parameters:
separator- the literal, nonempty string to recognize as a separator- Returns:
- a splitter, with default settings, that recognizes that separator
-
omitEmptyStrings
Returns a splitter that behaves equivalently tothissplitter, but automatically omits empty strings from the results. For example,Splitter.on(',').omitEmptyStrings().split(",a,,,b,c,,")returns an iterable containing only["a", "b", "c"].If either
trimResultsoption is also specified when creating a splitter, that splitter always trims results first before checking for emptiness. So, for example,Splitter.on(':').omitEmptyStrings().trimResults().split(": : : ")returns an empty iterable.- Returns:
- a splitter with the desired configuration
-
doNotModifySeparatorString
Usually the separator will be pre-processed before being passed toString.split(String). This is needed to mask special characters that will harmPattern.compile(String). If you want to disable this behavior and take care for your self you can change this method by calling this method.- Returns:
- a splitter with the desired configuration
-
trimResults
Returns a splitter that behaves equivalently tothissplitter, but automatically removes leading and trailing whitespace from each returned substring. For example,Splitter.on(',').trimResults().split(" a, b ,c ")returns an iterable containing["a", "b", "c"].- Returns:
- a splitter with the desired configuration
-
limit
Returns a splitter that behaves equivalently tothissplitter but stops splitting after it reaches the limit. The limit defines the maximum number of items returned by the iterator, or the maximum size of the list returned bysplitToList(java.lang.String).For example,
Splitter.on(',').limit(3).split("a,b,c,d")returns an iterable containing["a", "b", "c,d"]. When omitting empty strings, the omitted strings do not count. Hence,Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d")returns an iterable containing["a", "b", "c,d". When trim is requested, all entries are trimmed, including the last. HenceSplitter.on(',').limit(3).trimResults().split(" a , b , c , d ")results in["a", "b", "c , d"].- Parameters:
maxItems- the maximum number of items returned- Returns:
- a splitter with the desired configuration
-
splitToList
Splitssequenceinto string components and returns them as an immutable list.- Parameters:
sequence- the sequence of characters to split- Returns:
- an immutable list of the segments split from the parameter
-