


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.

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)

Recommended Books for Java Beginners
  • Head First Java 2nd Edition By Kathy Sierra (see here)
  • Head First Object-Oriented Analysis and Design (see here)

1 comment :

Post a Comment