Split a comma-separated String in Java

There are many ways we can split a comma-separated String in Java. In this tutorial, we will cover the following:

  • With a split() method
  • Using the Java 8 Streams API
  • Using the Splitter class from the Guava library

You might want to check the How to Split String in Java tutorial to see other examples. 

Split a comma-separated String with a split() method

We can use the String[] split(String regex) of the String class that splits the String around matches of the given regular expression. This method splits String into an array.

Example

public class SplitString {

  public static void main(String[] args) {

    String names = "Tom,Steve,John,Megan,Melissa";

    // split string
    String[] arr = names.split(",");

    // print the size of the array
    System.out.println(arr.length);

    // print the elements
    Stream.of(arr).forEach(System.out::println);

  }
}
Output: 5 Tom Steve John Megan Melissa
 
If you need a list, instead of an array, use the Arrays.asList() method:
 
  List<String> list = Arrays.asList(names.split(","));


And If you need an ArrayList, then use this code:

  List<String> list = new ArrayList<>(Arrays.asList(names.split(" , ")));


Note: If you have some whitespaces between commas and strings, you probably want to remove them.

To remove whitespaces, we need to provide the following regex: “\\s*,\\s*” as parameter to the split() method.

Example

  // String with whitespaces after commas
  String names = "Tom, Steve ,John, Megan, Melissa";

  // split the string and remove any whitespace
  List<String> = new ArrayList<>(Arrays.asList(names.split("\\s*,\\s*")));


Split the String using the Java 8 Streams API

It also can be done using the map() and collect() operators from the Java 8 Streams API.

Example

public class SplitString {

  public static void main(String[] args) {

    String names = "Tom,Steve,John,Megan,Melissa";

    List<String> = Stream.of(names.split(","))
	    .map(String::trim)
            .collect(Collectors.toList());

    System.out.println(list.size());

    list.forEach(System.out::println);

  }
}
Output: 5 Tom Steve John Megan Melissa

Using the Splitter class from the Guava library

The Splitter class from the guava library has some useful methods that we can use to split the String based on some separator.

To use it, you need to add the following dependency to your pom.xml:

<dependency>
    <groupid>com.google.guava</groupid>
    <artifactid>guava</artifactid>
    <version>31.0.1-jre</version>
</dependency>

Example:

public class SplitString {

  public static void main(String[] args) {

    String names = "Tom,Steve,John,Megan,Melissa";

    // returns an ArrayList
    List<String> = Lists.newArrayList(Splitter.on(",").split(names));

    System.out.println(list.size());

    list.forEach(System.out::println);

  }
}
Output: 5 Tom Steve John Megan Melissa
 
If you have whitespaces that you need to remove, then use the trimResults() method:
 
    // String with whitespaces after commas
    String names = "Tom, Steve, John, Megan, Melissa";

    // split the string and remove any whitespace
    List<String> = Lists.newArrayList(Splitter.on(",").trimResults().split(names));


That’s all about how to split a comma-separated String in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *