Split comma-separated Strings in Java

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)));
    }
}
Short URL for this post: http://blog.oio.de/GrN6a
This entry was posted in Java Basics and tagged , , , , , . Bookmark the permalink.

4 Responses to Split comma-separated Strings in Java

  1. Sharan says:

    Thanks for the tip! Was of great help!

  2. Junaid says:

    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?

  3. e2coder says:

    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.

Leave a Reply