CharMatcher instance. Or, instead of using a separator at all, a
splitter can extract adjacent substrings of a given fixed length.
For example, this expression:
Splitter.on(',').split("foo,bar,qux")
... produces an Iterable containing "foo", "bar" and "qux", in
that order.
By default, Splitter's behavior is simplistic and unassuming. The following
expression:
Splitter.on(',').split(" foo,,, bar ,")
... yields the substrings [" foo", "", "", " bar ", ""]. If this is not the desired
behavior, use configuration methods to obtain a new splitter instance with modified
behavior:
private static final Splitter MY_SPLITTER = Splitter.on(',')
.trimResults()
.omitEmptyStrings();
Now MY_SPLITTER.split("foo,,, bar ,") returns just ["foo", "bar"]. Note that
the order in which these configuration methods are called is never significant.
Warning
Splitter instances are immutable. Invoking a configuration method has no effect on the receiving instance; you must store and use the new splitter instance it returns instead.
// Do NOT do this
Splitter splitter = Splitter.on('/');
splitter.trimResults(); // does nothing!
return splitter.split("wrong / wrong / wrong");
For separator-based splitters that do not use omitEmptyStrings, an input string
containing n occurrences of the separator naturally yields an iterable of size n +
1. So if the separator does not occur anywhere in the input, a single substring is returned
containing the entire input. Consequently, all splitters split the empty string to [""]
(note: even fixed-length splitters).
Splitter instances are thread-safe immutable, and are therefore safe to store as static
final constants.
The Joiner class provides the inverse operation to splitting, but note that a
round-trip between the two should be assumed to be lossy.
See the Guava User Guide article on Splitter.
| Modifier and Type | Class and Description |
|---|---|
| public static class | Splitter.MapSplitter
An object that splits strings into maps as |
| private abstract static class | |
| private static interface |
| Modifier and Type | Field and Description |
|---|---|
| private final int | |
| private final boolean | |
| private final Splitter. | |
| private final CharMatcher |
| Access | Constructor and Description |
|---|---|
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static Splitter | Returns: a splitter, with default settings, that can split into fixed sized piecesthe desired length of pieces after splitting, a positive integer length)Returns a splitter that divides strings into pieces of the given length. |
| public Splitter | Returns: a splitter with the desired configurationthe maximum number of items returned maxItems)Returns a splitter that behaves equivalently to |
| public Splitter | Returns: a splitter with the desired configurationReturns a splitter that behaves equivalently to |
| public static Splitter | Returns: a splitter, with default settings, that recognizes that separatorthe character to recognize as a separator separator)Returns a splitter that uses the given single-character separator. |
| public static Splitter | Returns: a splitter, with default settings, that uses this matchera separatorMatcher)CharMatcher that determines whether a character is a
separatorReturns a splitter that considers any single character matched by the given |
| public static Splitter | |
| public static Splitter | |
| private static Splitter | |
| public static Splitter | Returns: a splitter, with default settings, that uses this patternthe pattern that determines whether a subsequence is a separator. This
pattern may not match the empty string. separatorPattern)Returns a splitter that considers any subsequence matching a given pattern (regular expression) to be a separator. |
| public Iterable | Returns: an iteration over the segments split from the parameterthe sequence of characters to split sequence)Splits |
| private Iterator | |
| public List | Returns: an immutable list of the segments split from the parameterthe sequence of characters to split sequence)Splits |
| public Stream | Returns: a stream over the segments split from the parameterthe sequence of characters to split sequence)Splits |
| public Splitter | Returns: a splitter with the desired configurationReturns a splitter that behaves equivalently to |
| public Splitter | Returns: a splitter with the desired configurationa trimmer)CharMatcher that determines whether a character should be removed from
the beginning/end of a subsequenceReturns a splitter that behaves equivalently to |
| public Splitter. | withKeyValueSeparator(String separator)
Returns a |
| public Splitter. | withKeyValueSeparator(char separator)
Returns a |
| public Splitter. | withKeyValueSeparator(Splitter keyValueSplitter)
Returns a |
| limit | back to summary |
|---|---|
| private final int limit | |
| omitEmptyStrings | back to summary |
|---|---|
| private final boolean omitEmptyStrings | |
| strategy | back to summary |
|---|---|
| private final Splitter. | |
| trimmer | back to summary |
|---|---|
| private final CharMatcher trimmer | |
| Splitter | back to summary |
|---|---|
| private Splitter(Splitter. | |
| Splitter | back to summary |
|---|---|
| private Splitter(Splitter. | |
| fixedLength | back to summary |
|---|---|
| public static Splitter fixedLength(final int length) Returns a splitter that divides strings into pieces of the given length. For example, Note if Exception: for consistency with separator-based splitters,
| |
| limit | back to summary |
|---|---|
| public Splitter limit(int maxItems) Returns a splitter that behaves equivalently to For example,
| |
| omitEmptyStrings | back to summary |
|---|---|
| public Splitter omitEmptyStrings() Returns a splitter that behaves equivalently to If either Note that it is ordinarily not possible for
| |
| on | back to summary |
|---|---|
| public static Splitter on(char separator) Returns a splitter that uses the given single-character separator. For example,
| |
| on | back to summary |
|---|---|
| public static Splitter on(final CharMatcher separatorMatcher) Returns a splitter that considers any single character matched by the given
| |
| on | back to summary |
|---|---|
| public static Splitter on(final String separator) Returns a splitter that uses the given fixed string as a separator. For example, | |
| on | back to summary |
|---|---|
| public static Splitter on(Pattern separatorPattern) Returns a splitter that considers any subsequence matching
| |
| on | back to summary |
|---|---|
| private static Splitter on(final CommonPattern separatorPattern) | |
| onPattern | back to summary |
|---|---|
| public static Splitter onPattern(String separatorPattern) Returns a splitter that considers any subsequence matching a given pattern (regular expression)
to be a separator. For example,
| |
| split | back to summary |
|---|---|
| public Iterable Splits
| |
| splittingIterator | back to summary |
|---|---|
| private Iterator | |
| splitToList | back to summary |
|---|---|
| public List Splits
| |
| splitToStream | back to summary |
|---|---|
| public Stream Splits
| |
| trimResults | back to summary |
|---|---|
| public Splitter trimResults() Returns a splitter that behaves equivalently to
| |
| trimResults | back to summary |
|---|---|
| public Splitter trimResults(CharMatcher trimmer) Returns a splitter that behaves equivalently to
| |
| withKeyValueSeparator | back to summary |
|---|---|
| public Splitter. Returns a
| |
| withKeyValueSeparator | back to summary |
|---|---|
| public Splitter. Returns a
| |
| withKeyValueSeparator | back to summary |
|---|---|
| public Splitter. Returns a Note Any configuration option configured on this splitter, such as Example:
| |
Splitter splits iterables and lists. Like
Splitter, it is thread-safe and immutable. The common way to build instances is by
providing an additional key-value separator to
Splitter.
| Modifier and Type | Field and Description |
|---|---|
| private final Splitter | |
| private static final String | |
| private final Splitter |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Map | split(CharSequence sequence)
Splits |
| entrySplitter | back to summary |
|---|---|
| private final Splitter entrySplitter | |
| INVALID_ENTRY_MESSAGE | back to summary |
|---|---|
| private static final String INVALID_ENTRY_MESSAGE | |
| outerSplitter | back to summary |
|---|---|
| private final Splitter outerSplitter | |
| MapSplitter | back to summary |
|---|---|
| private MapSplitter(Splitter outerSplitter, Splitter entrySplitter) | |
| split | back to summary |
|---|---|
| public Map Splits The returned map preserves the order of the entries from
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| pack-priv int | |
| pack-priv final boolean | |
| pack-priv final CharSequence | |
| pack-priv final CharMatcher |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected String | |
| pack-priv abstract int | separatorEnd(int separatorPosition)
Returns the first index in |
| pack-priv abstract int | separatorStart(int start)
Returns the first index in |
| limit | back to summary |
|---|---|
| pack-priv int limit | |
| offset | back to summary |
|---|---|
| pack-priv int offset | |
| omitEmptyStrings | back to summary |
|---|---|
| pack-priv final boolean omitEmptyStrings | |
| toSplit | back to summary |
|---|---|
| pack-priv final CharSequence toSplit | |
| trimmer | back to summary |
|---|---|
| pack-priv final CharMatcher trimmer | |
| SplittingIterator | back to summary |
|---|---|
| protected SplittingIterator(Splitter splitter, CharSequence toSplit) | |
| computeNext | back to summary |
|---|---|
| protected String computeNext() Implements abstract com.
| |
| separatorEnd | back to summary |
|---|---|
| pack-priv abstract int separatorEnd(int separatorPosition) Returns the first index in | |
| separatorStart | back to summary |
|---|---|
| pack-priv abstract int separatorStart(int start) Returns the first index in | |
| iterator | back to summary |
|---|---|
| public Iterator | |