/** * 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 */ 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! 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 not * 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 are * trimmed, including the last. 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 */ 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 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 */ public Splitter omitEmptyStrings() { return new Splitter(strategy, true, 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 */ 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; } }; } }); }
/** * 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 */ public Splitter omitEmptyStrings() { return new Splitter(strategy, true, trimmer, limit); }
checkArgument(length > 0, "The length may not be less than 1"); return new Splitter( new Strategy() { @Override
/** * 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! 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 not * 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 are * trimmed, including the last. 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 */ 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 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! public Splitter trimResults(CharMatcher trimmer) { checkNotNull(trimmer); 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 */ 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; } }; } }); }
return Splitter.on(separator.charAt(0)); return new Splitter( new Strategy() { @Override
/** * 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 not * 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 are * trimmed, including the last. 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 */ public Splitter limit(int limit) { checkArgument(limit > 0, "must be greater than zero: %s", limit); return new Splitter(strategy, omitEmptyStrings, trimmer, limit); }
checkArgument(length > 0, "The length may not be less than 1"); return new Splitter( new Strategy() { @Override
/** * 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 */ 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
private static Splitter on(final CommonPattern separatorPattern) { checkArgument( !separatorPattern.matcher("").matches(), "The pattern may not match the empty string: %s", separatorPattern); return new Splitter( new Strategy() { @Override public SplittingIterator iterator(final Splitter splitter, CharSequence toSplit) { final CommonMatcher matcher = separatorPattern.matcher(toSplit); return new SplittingIterator(splitter, toSplit) { @Override public int separatorStart(int start) { return matcher.find(start) ? matcher.start() : -1; } @Override public int separatorEnd(int separatorPosition) { return matcher.end(); } }; } }); }
return Splitter.on(separator.charAt(0)); return new Splitter( new Strategy() { @Override
private static Splitter on(final CommonPattern separatorPattern) { checkArgument( !separatorPattern.matcher("").matches(), "The pattern may not match the empty string: %s", separatorPattern); return new Splitter( new Strategy() { @Override public SplittingIterator iterator(final Splitter splitter, CharSequence toSplit) { final CommonMatcher matcher = separatorPattern.matcher(toSplit); return new SplittingIterator(splitter, toSplit) { @Override public int separatorStart(int start) { return matcher.find(start) ? matcher.start() : -1; } @Override public int separatorEnd(int separatorPosition) { return matcher.end(); } }; } }); }
private static Splitter on(final CommonPattern separatorPattern) { checkArgument( !separatorPattern.matcher("").matches(), "The pattern may not match the empty string: %s", separatorPattern); return new Splitter( new Strategy() { @Override public SplittingIterator iterator(final Splitter splitter, CharSequence toSplit) { final CommonMatcher matcher = separatorPattern.matcher(toSplit); return new SplittingIterator(splitter, toSplit) { @Override public int separatorStart(int start) { return matcher.find(start) ? matcher.start() : -1; } @Override public int separatorEnd(int separatorPosition) { return matcher.end(); } }; } }); }