Convert comma separated String to List

List<String> items = Arrays.asList(str.split("\\s*,\\s*"));

The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas.


Please note that this returns simply a wrapper on an array: you CANNOT for example .remove() from the resulting List. For an actual ArrayList you must further use new ArrayList<String>.

Answer from AlexR on Stack Overflow
🌐
LabEx
labex.io › tutorials › java-how-to-split-a-string-into-an-arraylist-using-a-delimiter-in-java-415655
How to split a string into an ArrayList using a delimiter in Java | LabEx
Java provides a built-in method called split() in the String class that allows us to divide a string into parts based on a delimiter. The method returns an array of strings containing the substrings divided by the delimiter.
Discussions

java - How to split a string on multiple delimiters stored in a list? - Stack Overflow
You can use replaceAll on the string first and convert it all to a fixed delimiter (your choice) like this - then you can split it based on that: More on stackoverflow.com
🌐 stackoverflow.com
May 29, 2020
java - Splitting strings based on a delimiter - Stack Overflow
I still suggest to use split(), it skips null tokens by default. you want to get rid of non numeric characters in the string and only keep pipes and numbers, then you can easily use split() to get what you want. or you can pass multiple delimiters to split (in form of regex) and this should work: ... import java... More on stackoverflow.com
🌐 stackoverflow.com
Best way to split a string with multiple delimiters
If you just need to split the string, you can first split by |, then split all substrings by &&. More on reddit.com
🌐 r/C_Programming
12
7
July 14, 2024
How to split a string without a delimiter (KOTLIN)
Consider whether or not it's necessary to involve regex. If your input is always 14 digits, and your output is always ###-#######-####, then you can solve this pretty simply with substrings. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/substring.html You can provide the start and end index to extract a substring from a string. Then you just combine them back together with dashes in between each substring, and you're done. No need for the overhead of regex at all. More on reddit.com
🌐 r/learnprogramming
6
3
October 7, 2022
🌐
Blogger
javarevisited.blogspot.com › 2017 › 01 › how-to-split-string-based-on-delimiter-in-java.html
How to Split String based on delimiter in Java? Example Tutorial
This is similar to the earlier example but it's more common because CSV is a popular format to export data from databases, tables, or XLS files. You can split a comma-delimited String by passing "," to split() method as shown in the following ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-a-string-in-java-with-delimiter
How to Split a String in Java with Delimiter? - GeeksforGeeks
July 23, 2025 - In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter.
🌐
Codecademy
codecademy.com › docs › java › strings › .split()
Java | Strings | .split() | Codecademy
March 30, 2022 - The .split() method takes a string and splits it into an array of substrings based on a pattern delimiter. The pattern provided is a regular expression that will split the original string based on where the pattern is matched in the string.
🌐
Baeldung
baeldung.com › home › java › java string › java string.split()
Java.String.split() | Baeldung
July 15, 2026 - In this tutorial, we’ll learn about the String.split() method in Java. This method helps us break a string into smaller pieces, based on a specified delimiter. A delimiter is a character or a sequence of characters that mark the boundaries ...
🌐
How to do in Java
howtodoinjava.com › home › string › split a string in java: split(), pattern, scanner and more
Split a String in Java: split(), Pattern, Scanner and More
3 weeks ago - The split() method treats its argument as a regular expression and cuts the string at every match. It is the shortest way to split a string, and it is the right default when the delimiter is fixed and we want an array.
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java | mkyong.com
February 9, 2022 - The example uses the regex pattern \\r?\\n to split a string by new lines and return a stream; later, we trim and filter the empty lines. ... package com.mkyong.string.split; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StringSplitNewLines { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("aaa \n"); sb.append("bbb \r\n"); sb.append("ccc\n"); sb.append("\n"); sb.append("ddd\r\n"); sb.append("\r\n"); sb.append("eee\n"); String text = sb.toString(); System.out.println("---Original---"); System.out.println(text); System.out.println("---Split---"); // split by new line, trim and filter empty line List<String> lines = Arrays.stream(text.split("\\r?\\n")) .map(x -> x.trim()) .filter(x -> x.length() > 0) .collect(Collectors.toList()); for (String line : lines) { System.out.println(line); } } }
🌐
Java67
java67.com › 2017 › 09 › how-to-convert-comma-separated-string-to-ArrayList-in-java-example.html
How to Convert a Comma Separated String to an ArrayList in Java - Example Tutorial | Java67
Well, Java doesn't provide any such constructor or factory method to create ArrayList from delimited String, but you can use String.split() method and Arrays.asList() method together to create an ArrayList from any delimited String, not just ...
🌐
javathinking
javathinking.com › blog › convert-string-separate-by-to-list-java
Converting a Delimiter-Separated String to a List in Java — javathinking.com
When a user enters a list of values separated by a delimiter, converting the input string to a list enables you to perform operations on each value independently. If the delimiter appears at the beginning, end, or multiple times consecutively ...
🌐
YouTube
youtube.com › oggi ai - artificial intelligence today
Java: Split String at Delimiter | break String into Array of Substrings - YouTube
This video explains how to split a string in Java into an array of substrings, at one or more delimiter characters such as space, period, or comma. Code: htt...
Published: April 20, 2023
Views: 1K
🌐
Quora
quora.com › How-would-I-split-a-string-using-n-as-a-delimiter-character-and-put-the-split-strings-into-a-linked-list
How would I split a string using '\n' as a delimiter character and put the split strings into a linked list? - Quora
Answer (1 of 4): You can split a String into an array of Strings for a given delimiter using the split() method in Java. [code]String input = "I am a walrus\nYou don't know me\nGood for you."; String[] splitArray = input.split("\n"); [/code]This will split the string into an array of strings usi...
🌐
Attacomsian
attacomsian.com › blog › java-string-with-separator-to-list
Convert a comma-separated string to a list in Java
October 29, 2022 - The String class in Java provides split() method to split a string into an array of strings.
🌐
How to do in Java
howtodoinjava.com › home › string › java string split(): limit, regex escaping and empty strings
Java String split() : Splitting by One or Multiple Delimiters
October 12, 2023 - The Java String split() method breaks a string around every match of a regular expression and returns the parts as a String array, and the overload split(regex, limit) also controls how many parts we get and whether trailing empty strings stay in the result. The delimiter is always a regex, so characters such as . and | need escaping. We split strings to read CSV rows, break a sentence into words, parse key-value settings or take apart log lines and file paths.
🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
How to split a string in Java
September 23, 2026 - public class Main { public static void main(String[] arg) { String str = "how:to:split:a:string:in:java"; String[] arrOfStr = str.split(":"); for (String a : arrOfStr) { System.out.println(a); } } } ... In the example above, we use : as the delimiter, but note that the parameter the split() method takes is a regular expression. This means it is easy to do things like split on one or more : characters by using :+.
🌐
Codefinity
codefinity.com › courses › v2 › 0bfa1111-8677-478f-bcd3-315dff1d7d40 › 421ab035-77e8-4fef-abb9-488c91c2d1f9 › 433c4bac-9f33-4b48-9be0-cdc81423c941
Learn Method split() | String Advanced
The split(String delimiter) method splits a string into an array of substrings based on the specified delimiter (a character or sequence of characters). It returns an array where each element is a substring from the original string, separated ...
🌐
Baeldung
baeldung.com › home › java › java string › convert a comma separated string to a list in java
Convert a Comma Separated String to a List in Java
September 3, 2025 - To convert our countries string, we’ll first call Splitter.on with a comma as the parameter to specify what character our string should be split on. Then, we’ll use the trimResults method on our Splitter instance.