Sunday, December 10, 2017

How to split a comma separated String in Java? Regular Expression Example

You can use String.split() function or StringTokenizer class to split a comma separated String in Java. Since splitting a String is a very common functionality, Java designers have provided a couple of split() method on java.lang.String class itself. These split() function takes a regular expression and split the String accordingly. In order to parse a comma delimited String, you can just provide a "," as a delimiter and it will return an array of String containing individual values. The split() function internally uses Java's regular expression API (java.util.regex) to do its job. If you are not very familiar with the regular expression than you can also use StringTokenizer class, which can also split a comma delimited String but StringTokenizer is an old class and not recommended and you should try to use the split() function from java.lang.String class, as any performance improvement will likely to happen on this method than the StringTokenizer class. Let's see a couple of examples to split a comma separated String in Java. You can also see Java Regular Expressions, taming the java.util.regex engine to learn the full power of Java regular expression.

Split a comma delimited String into array

This is the simplest example of splitting a CSV String in Java. All you need to do is call the split() function with delimiter as shown in the following example. This method splits the String based upon given regular expression and return a String array with individual String values.

package beginner;

import java.util.Arrays;

 * Java Program to split a CSV String into individual String values. This
 * program shows two ways to split the CSV String and create a String array out
 * of it, first by using String.split() method and second, by using
 * StringTokenizer class.
 * @author WINDOWS 8
public class HelloWorldApp {

    public static void main(String... args) {

        String CSV = "Google,Apple,Microsoft";

        String[] values = CSV.split(",");


Output :[Google, Apple, Microsoft]

You can also create an ArrayList by splitting a comma separated String as shown below:

ArrayList list = new ArrayList(Arrays.asList(values)

If your comma separated String also contains whitespace between values e.g. "Google, Microsoft, Apple" then you can use the following regular expression to split the CSV string as well as get rid of the leading and trailing whitespaces from individual values.

String CSV = "Google, Apple, Microsoft";

String[] values = CSV.split("\\s*,\\s*");


Here \\s* is the regular expression to find zero or more space. \s is the metacharacter to find whitespace including tabs. since \ (forward slash) requires escaping in Java it becomes \\ (double slash) and \s becomes \\s. Now coming to * (star or asterisk), it's another special character in regular expression which means any number of times. So \\s* means space any number of times.

How to split a comma separated String in Java

Split Comma Separated String using StringTokenizer in Java

And, here is the example of how you can use StringTokenizer to split a comma separated String into String array. StringTokenizer is a utility class in java.util package, which accepts a String and breaks into tokens. By default, StringTokenizer breaks the String on whitespace but you can pass the token you want to use. For example, to break a comma separated String into individual tokens, we can pass comma (,) as token as shown in the following example. Once you do that, you can just iterate over StringTokenizer using hasMoreTokens() and retrieve values using nextToken(), similar to how you iterate over ArrayList using Iterator.

public class HelloWorldApp {

    public static void main(String... args) {

        String CSV = "Google,Apple,Microsoft";

        StringTokenizer tokenizer = new StringTokenizer(CSV, ",");
        while (tokenizer.hasMoreTokens()) {


That's all about how to split a comma separated String in Java. You can use the split() function to parse comma delimited String. Just remember that it takes regular expression but to break CSV string, you just need to pass "," if your String is like "a,b,c" i.e. there is no space between two values. If there is space between values then you can use regular expression "\\s*,\\s*" to remove the space around individual values.

Further Reading
Complete Java Masterclass
Regular Expression Fundamentals
Java In-Depth: Become a Complete Java Engineer!

Other Java String tutorials from this blog:
  • How to reverse String in Java using recursion? (solution)
  • How to compare two Sring in Java? (solution)
  • How to format String in Java? (example)
  • How to convert double to String in Java? (solution)
  • How to check if two String are Anagram? (solution)
  • How to convert Date to String in Java? (answer)
  • How String in switch case works in Java? (answer)


Unknown said...

Nice post

Partha Pratim Sanyal said...

Doesnt work

javin paul said...

Hello PArtha, what problem you are getting? can you share more?

Post a Comment