/** * Returns a splitter that behaves equivalently to {@code this} splitter, but * automatically omits empty strings from the results. For example, {@code * Splitter.on(',').omitEmptyStrings().split(",a,,,b,c,,")} returns an * iterable containing only {@code ["a", "b", "c"]}. * * <p>If either {@code trimResults} option is also specified when creating a * splitter, that splitter always trims results first before checking for * emptiness. So, for example, {@code * Splitter.on(':').omitEmptyStrings().trimResults().split(": : : ")} returns * an empty iterable. * * <p>Note that it is ordinarily not possible for {@link #split(CharSequence)} * to return an empty iterable, but when using this option, it can (if the * input sequence consists of nothing but separators). * * @return a splitter with the desired configuration */ @CheckReturnValue public Splitter omitEmptyStrings() { return new Splitter(strategy, true, trimmer, limit); }
/** * Returns a splitter that behaves equivalently to {@code this} splitter, but * removes all leading or trailing characters matching the given {@code * CharMatcher} from each returned substring. For example, {@code * Splitter.on(',').trimResults(CharMatcher.is('_')).split("_a ,_b_ ,c__")} * returns an iterable containing {@code ["a ", "b_ ", "c"]}. * * @param trimmer a {@link CharMatcher} that determines whether a character * should be removed from the beginning/end of a subsequence * @return a splitter with the desired configuration */ // TODO(kevinb): throw if a trimmer was already specified! @CheckReturnValue public Splitter trimResults(CharMatcher trimmer) { checkNotNull(trimmer); return new Splitter(strategy, omitEmptyStrings, trimmer, limit); }
/** * Returns a splitter that behaves equivalently to {@code this} splitter 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 by {@link #splitToList}. * * <p>For example, * {@code Splitter.on(',').limit(3).split("a,b,c,d")} returns an iterable * containing {@code ["a", "b", "c,d"]}. When omitting empty strings, the * omitted strings do no count. Hence, * {@code Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d")} * returns an iterable containing {@code ["a", "b", "c,d"}. * When trim is requested, all entries, including the last are trimmed. Hence * {@code Splitter.on(',').limit(3).trimResults().split(" a , b , c , d ")} * results in {@code ["a", "b", "c , d"]}. * * @param limit the maximum number of items returned * @return a splitter with the desired configuration * @since 9.0 */ @CheckReturnValue public Splitter limit(int limit) { checkArgument(limit > 0, "must be greater than zero: %s", limit); return new Splitter(strategy, omitEmptyStrings, trimmer, limit); }
/** * Returns a splitter that considers any single character matched by the * given {@code CharMatcher} to be a separator. For example, {@code * Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux")} returns an * iterable containing {@code ["foo", "", "bar", "quux"]}. * * @param separatorMatcher a {@link CharMatcher} that determines whether a * character is a separator * @return a splitter, with default settings, that uses this matcher */ @CheckReturnValue public static Splitter on(final CharMatcher separatorMatcher) { checkNotNull(separatorMatcher); return new Splitter( new Strategy() { @Override public SplittingIterator iterator(Splitter splitter, final CharSequence toSplit) { return new SplittingIterator(splitter, toSplit) { @Override int separatorStart(int start) { return separatorMatcher.indexIn(toSplit, start); } @Override int separatorEnd(int separatorPosition) { return separatorPosition + 1; } }; } }); }
checkArgument(length > 0, "The length may not be less than 1"); return new Splitter( new Strategy() { @Override
return Splitter.on(separator.charAt(0)); return new Splitter( new Strategy() { @Override
separatorPattern); return new Splitter( new Strategy() { @Override