/** * Returns a splitter that uses the given single-character separator. For example, {@code * Splitter.on(',').split("foo,,bar")} returns an iterable containing {@code ["foo", "", "bar"]}. * * @param separator the character to recognize as a separator * @return a splitter, with default settings, that recognizes that separator */ public static Splitter on(char separator) { return on(CharMatcher.is(separator)); }
/** * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries * into keys and values using the specified separator. * * @since 14.0 */ @Beta public MapSplitter withKeyValueSeparator(char separator) { return withKeyValueSeparator(on(separator)); }
/** * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries * into keys and values using the specified separator. * * @since 10.0 */ @Beta public MapSplitter withKeyValueSeparator(String separator) { return withKeyValueSeparator(on(separator)); }
/** * Returns a splitter that considers any subsequence matching {@code pattern} to be a separator. * For example, {@code Splitter.on(Pattern.compile("\r?\n")).split(entireFile)} splits a string * into lines whether it uses DOS-style or UNIX-style line terminators. * * @param separatorPattern the pattern that determines whether a subsequence is a separator. This * pattern may not match the empty string. * @return a splitter, with default settings, that uses this pattern * @throws IllegalArgumentException if {@code separatorPattern} matches the empty string */ @GwtIncompatible // java.util.regex public static Splitter on(Pattern separatorPattern) { return on(new JdkPattern(separatorPattern)); }
/** * Returns a splitter that considers any subsequence matching a given pattern (regular expression) * to be a separator. For example, {@code Splitter.onPattern("\r?\n").split(entireFile)} splits a * string into lines whether it uses DOS-style or UNIX-style line terminators. This is equivalent * to {@code Splitter.on(Pattern.compile(pattern))}. * * @param separatorPattern the pattern that determines whether a subsequence is a separator. This * pattern may not match the empty string. * @return a splitter, with default settings, that uses this pattern * @throws IllegalArgumentException if {@code separatorPattern} matches the empty string or is a * malformed expression */ @GwtIncompatible // java.util.regex public static Splitter onPattern(String separatorPattern) { return on(Platform.compilePattern(separatorPattern)); }
checkArgument(separator.length() != 0, "The separator may not be the empty string."); if (separator.length() == 1) { return Splitter.on(separator.charAt(0));
/** * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain * System#getProperty system property}. */ @VisibleForTesting // TODO(b/65488446): Make this a public API. static ImmutableList<URL> parseJavaClassPath() { ImmutableList.Builder<URL> urls = ImmutableList.builder(); for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { try { try { urls.add(new File(entry).toURI().toURL()); } catch (SecurityException e) { // File.toURI checks to see if the file is a directory urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { logger.log(WARNING, "malformed classpath entry: " + entry, e); } } return urls.build(); }
Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname); List<String> path = new ArrayList<>();
/** * Converts a field mask from a Proto3 JSON string, that is splitting the paths along commas and * converting from camel case to snake case. */ public static FieldMask fromJsonString(String value) { Iterable<String> paths = Splitter.on(FIELD_PATH_SEPARATOR).split(value); FieldMask.Builder builder = FieldMask.newBuilder(); for (String path : paths) { if (path.isEmpty()) { continue; } builder.addPaths(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, path)); } return builder.build(); }