Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Split CSV String using Regex in Java

Learn to split string by comma or space and store in array or arraylist. Use given Java program to convert string to List in Java.

Lokesh Gupta

February 22, 2023

Java String class
CSV Files, Java String

Learn to split a CSV (comma-separated value) and store the tokens in an array or List in Java using simple and easy-to-follow examples.

1. Split CSV with Regular Expression

We can use a regular expression "\\s*,\\s*" to match commas in CSV string and then use String.split() method to convert string to an array of tokens.

String blogName = "how, to, do, in, java";
String[] tokenArray = blogName.split("\\s*,\\s*");

Assertions.assertArrayEquals(new String[]{"how", "to", "do", "in", "java"}, tokenArray);

Note that the rex takes care of extra unwanted spaces in the String and only tokenizes the proper strings. In the following example, notice the unwanted multiple spaces between the commas.

Assertions.assertArrayEquals(new String[]{"a", "b", "c"}, "a, b,c".split("\\s*,\\s*")); 
Assertions.assertArrayEquals(new String[]{"a", "b", "c"}, "a,   b,    c".split("\\s*,\\s*"));

3. Convert Array to List

To get the List of tokens, we can pass the array to Arrays.asList() method that returns a fixed-size, unmodifiable read-only list backed by the array.

String[] tokenArray = blogName.split("\\s*,\\s*");

List<String> tokenList = Arrays.asList(tokenArray);

To get the mutable ArrayList, copy all elements from the read-only list received from the above example into a new ArrayList object.

ArrayList<String> tokenArrayList = new ArrayList(Arrays.asList(tokenArray));

3. Convert a List to CSV String

If we want to convert a List to CSV, then we can use String.join() method provided by Java 8.

List<String> list = Arrays.asList("how", "to", "do", "in", "java");
String result = String.join(",", list);    //delimited by comma
Assertions.assertEquals("how,to,do,in,java", result);

Above examples will help to convert CSV to List and List to CSV in Java.

Happy Learning !!

Read More:

  • Read/Write CSV file with OpenCSV
  • Read/Write CSV file with SuperCSV
  • 3 examples of parsing CSV files
  • Join String in Java 8

Sourcecode on Github

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java Custom Serialization using readObject() and writeObject()

Next

Java Enum with Strings

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap