Shortly, I saw some code like this to work with comma separated lists:
String [] elements = "Hello, World".split(","); for (String element : elements) { String trimmedElement = element.trim(); // do something with trimmedElement }
The code uses String.split(“,”) to split the strings at commas and String.trim() to remove unwanted whitespaces.
But theres’s a much more elegant way to do that in one step: Regexes. Fortunately, String.split(“,”) directly supports these. A simple Regex to ignore all spaces around the comma would look like this: ” *, “. To support other whitespaces, this can be changed to: “\s,\s*”. Some testcases for this:
public class CommaSeparatedStringTest { public static final String REGEX = "\s*,\s*"; @Test public void testSplit() { Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello,World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello, World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello ,World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello , World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hel lo", "World"}, "Hel lo,World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hellot,World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello,rWorld".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hellot ,rnWorld".split(REGEX))); } }
But there’s more possible … If you even want to remove empty elements/double commas (e.g. “Hello,,World”), you can use the following Regex: “[\s],[,\s]“. Again some testcases for this Regex:
public class CommaSeparatedStringTest { public static final String REGEX = "\s*,[,\s]*"; @Test public void testSplit() { Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello,,World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hello", "World"}, "Hello, ,World".split(REGEX))); Assert.assertTrue(Arrays.equals(new String []{"Hel lo", "World"}, "Hel lot, ,rWorld".split(REGEX))); } }
Thanks for the tip! Was of great help!
This will not work if we have empty element in the the beginning…
For e.g.: “,,Hello,World,,”;
will return String array of length 3 i.e.
1.
2. Hello
3. World.
How would you fix this issue?
I also have the same issue.
Java 8:
// someStringValue holds a non-null string value
result = Arrays.stream(someStringValue.split(“,”)).map(String::trim).filter(s -> !s.isEmpty()).distinct().sorted().toArray(String[]::new);
// Will split on commas, trim each result, remove empty strings, remove duplicates, and return an array of the strings in (natural) sort order.
Cool thanks, that’s a nice one!
Another option is to use Google Guava (https://code.google.com/p/guava-libraries/):
List splitResult = com.google.common.collect.Lists.newArrayList(
com.google.common.base.Splitter.on(‘,’)
.omitEmptyStrings()
.trimResults()
.split(“Hello , , World,”));
assertThat(splitResult, is(Arrays.asList(“Hello”, “World”)));