C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
On: This method has many possible arguments. We can pass a CharMatcher or a regular expression pattern—or just a character.
List: SplitToList returns a List of the strings. This can be transformed to an ArrayList or used directly in a loop.
Java program that uses Splitter, splitToList
import java.util.List;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String value = "cup,tea,coffee";
// Use Splitter, on method, and splitToList.
// ... Separates String on comma.
List<String> list = Splitter.on(',').splitToList(value);
for (String element : list) {
System.out.println(element);
}
}
}
Output
cup
tea
coffee
Tip: TrimResults does not modify existing Strings. It takes effect before Strings exist—this can improve performance and reduce memory use.
Iterable: This example uses the Iterable result, which split() returns. This is easy to loop over, but is not a list collection.
Java program that uses TrimResults, split
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String value = "rock ,stone, bird, fish";
// Split on comma, and trim our results.
// ... Use split, which returns an Iterable.
Iterable<String> result = Splitter.on(',').trimResults().split(value);
for (String v : result) {
System.out.println(v);
}
}
}
Output
rock
stone
bird
fish
With omitEmptyStrings: The result of the Splitter call is modified. So those empty entries are never added to our result list or Iterable.
Java program that uses omitEmptyStrings
import java.util.List;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String values = "cat,,dog,,,fish";
// Omit empty Strings when splitting.
List<String> list = Splitter.on(',').omitEmptyStrings()
.splitToList(values);
System.out.println(list.toString());
}
}
Output
[cat, dog, fish]
So: Using CharMatcher.WHITESPACE is a good way to split on any whitespace characters in the input string.
Java program that uses CharMatcher, Splitter
import java.util.List;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String values = "hey, how\nare you\tdoing my friend?";
// Split on whitespace with CharMatcher class.
List<String> results = Splitter.on(CharMatcher.WHITESPACE)
.omitEmptyStrings().splitToList(values);
for (String value : results) {
System.out.println(value);
}
}
}
Output
hey,
how
are
you
doing
my
friend?
CharMatcher options
ANY
ASCII
DIGIT
INVISIBLE
JAVA_DIGIT
JAVA_ISO_CONTROL
JAVA_LETTER
JAVA_LETTER_OR_DIGIT
JAVA_LOWER_CASE
JAVA_UPPER_CASE
NONE
SINGLE_WIDTH
WHITESPACE
CharSequence: We use a String as the CharSequence argument. Our matcher will split on comma, semicolon, and colon chars.
Java program that uses CharMatcher.anyOf method
import java.util.List;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String values = "one,two:three;four";
// Split with the CharMatcher.anyOf method.
// ... We split on comma, semicolon and colon chars.
List<String> results = Splitter.on(CharMatcher.anyOf(",;:"))
.splitToList(values);
for (String value : results) {
System.out.println(value);
}
}
}
Output
one
two
three
four
Tip: The example will split on the chars 0, 1, 2 and 3 inclusive. It will not split on 4.
Result: The entry "dog 4" is considered a valid string, unlike "cat 0." We have exact control over our delimiters.
Java program that uses CharMatcher.inRange
import java.util.List;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String values = "cat 0 dog 4 1 fish";
// Split on a range of characters.
// ... We split on 0, 1, 2 and 3 but not 4.
// ... We trim our resulting strings.
List<String> results = Splitter.on(CharMatcher.inRange('0', '3'))
.trimResults().splitToList(values);
for (String value : results) {
System.out.println(value);
}
}
}
Output
cat
dog 4
fish
Note: We pass the separator within pairs to this method. The on() method receives the separator between pairs.
Java program that uses withKeyValueSeparator
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
// This String contains key-value pairs.
String value = "cat=Fluffy,dog=Spot,bird=Chirpy";
// Use Splitter to parse key-value separators.
Map<String, String> map = Splitter.on(',').withKeyValueSeparator('=')
.split(value);
// Loop over entries.
for (Entry<String, String> entry : map.entrySet()) {
System.out.println("[" + entry.getKey() + "] " + entry.getValue());
}
}
}
Output
[cat] Fluffy
[dog] Spot
[bird] Chirpy
Java program that uses Splitter with HashMap
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String pairs = "cat:10 dog:4 fish:2";
// Separate pairs on spaces, and use colon as key-value separator.
Map<String, String> map = Splitter.on(' ').withKeyValueSeparator(':')
.split(pairs);
// Create HashMap from Map.
HashMap<String, String> hash = new HashMap<>(map);
// Display some values.
System.out.println(hash.get("cat"));
System.out.println(hash.get("dog"));
System.out.println(hash.get("fish"));
}
}
Output
10
4
2
Note: For simple programs like this one, please consider using the split() method included with Java.
Java program that uses Splitter with ArrayList
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String input = "jeans/pants/shirt/socks";
// Use Splitter to get ArrayList of Strings.
List<String> list = Splitter.on('/').splitToList(input);
ArrayList<String> result = new ArrayList<>(list);
System.out.println(result.toString());
}
}
Output
[jeans, pants, shirt, socks]
Java program that uses toArray, splitToList
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String data = "100:200:300:400";
// Convert result of splitToList to a String array.
String[] result = new String[0];
result = Splitter.on(':').splitToList(data).toArray(result);
// Display our results.
for (String value : result) {
System.out.println(value);
}
}
}
Output
100
200
300
400
However: If a program requires trim() calls, or more advanced capabilities of Splitter, the results would likely be different.
And: For a fast implementation of complex splits, a developer might end up with something resembling Splitter.
Java program that times Splitter, Java split method
import com.google.common.base.Splitter;
public class Program {
public static void main(String[] args) {
String value = "one,two,three,four,five,six";
int count1 = 0;
int count2 = 0;
long t1 = System.currentTimeMillis();
// Version 1: use Splitter from Guava.
for (int i = 0; i < 1000000; i++) {
Iterable<String> results = Splitter.on(',').split(value);
for (String v : results) {
count1 += v.length();
}
}
long t2 = System.currentTimeMillis();
// Version 2: use split from Java.
for (int i = 0; i < 1000000; i++) {
String[] results = value.split(",");
for (String v : results) {
count2 += v.length();
}
}
long t3 = System.currentTimeMillis();
// ... Counts.
System.out.println(count1);
System.out.println(count2);
// ... Times.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
22000000
22000000
389 ms: Splitter.on (Guava)
304 ms: split (Java)