You could do this:

String str = "...";
List<String> elephantList = Arrays.asList(str.split(", "));

In Java 9+, create an unmodifiable list with List.of.

List<String> elephantList = List.of(str.split(", "));  // Unmodifiable list. 

Basically the .split() method will split the string according to (in this case) delimiter you are passing and will return an array of strings.

However, you seem to be after a List of Strings rather than an array. So the array must be turned into a list. We can turn that array into a List object by calling either Arrays.asList() or List.of.


FYI you could also do something like so:

String str = "...";
ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));

But it is usually better practice to program to an interface rather than to an actual concrete implementation. So I would not recommend this approach.

Answer from npinti on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-split-string-by-comma
Java - Split String by Comma - GeeksforGeeks
July 23, 2025 - In Java, splitting a string by a comma is commonly achieved using the split() method of the String class. This method takes a regular expression as an argument and returns an array of substrings split at each match of the regex.
Discussions

In Java, how would I structure my regex to do a string split but only on commas, or commas followed by whitespace?
Use an optional: ",\s?" This means: "a comma, followed by 0 or 1 whitespace char". If you must allow for more than 1 whitespace, use ",\s*". The "*" means "0 or more". A good regex reference I know is: https://www.regular-expressions.info/ More on reddit.com
🌐 r/learnprogramming
15
4
November 24, 2021
performance - Quickest way to split a delimited String in Java - Software Engineering Stack Exchange
I am building a Comparator that provides multi-column sort capability on a delimited String. I am currently using the split method from String class as my preferred choice for splitting the raw Str... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
December 20, 2013
java - Split a string with arbitrary number of commas and spaces - Stack Overflow
You do not only want to include ... to split on them as one unit: ... Save this answer. ... Show activity on this post. ... Save this answer. ... Show activity on this post. Matching any chars other than commas and spaces is likely to be a cleaner solution: ... If you're working in Javascript you could also use the Lodash _.words method (kudos to them for the above regex): ... Find the answer to your question by ... More on stackoverflow.com
🌐 stackoverflow.com
Splitting Strings on Comma With Embedded Commas
You can use lookarounds to add custom assertions. Note that your input/output doesn't seem to match, perhaps there's no space between Cat3 and Cat4? >>> import re >>> s = 'Cat1,Cat2, Big,Cat3, Cat4, Small,Cat5' >>> re.split(r',(?=[A-Z])', s) ['Cat1', 'Cat2, Big', 'Cat3, Cat4, Small', 'Cat5'] >>> s = 'Cat1,Cat2, Big,Cat3,Cat4, Small,Cat5' >>> re.split(r',(?=[A-Z])', s) ['Cat1', 'Cat2, Big', 'Cat3', 'Cat4, Small', 'Cat5'] More on reddit.com
🌐 r/learnpython
4
12
December 8, 2019
🌐
Medium
medium.com › @AlexanderObregon › splitting-a-string-by-comma-in-java-the-right-way-1d3aa2aff1fa
Splitting a String by Comma in Java the Right Way | Medium
June 26, 2025 - It takes a string argument that’s treated as a regular expression and breaks up the original string using whatever matches that pattern. ... That gives you an array with three values: "alpha", "beta", and "gamma". The split works here because the comma doesn't carry any special behavior in regular expressions, so Java reads it as just another character in the string.
🌐
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 - In both these cases, we use the split utility method from the String class to split the comma-separated string into a string array.
🌐
Reddit
reddit.com › r/learnprogramming › in java, how would i structure my regex to do a string split but only on commas, or commas followed by whitespace?
r/learnprogramming on Reddit: In Java, how would I structure my regex to do a string split but only on commas, or commas followed by whitespace?
November 24, 2021 -

**And round braces

Basically I have the following string split, but it breaks for multi word names. This is an example of the structure:

Sao Paulo, (-23.55, -46.63)

Where sArr is a string array, I attempted to match commas, whitespace and brackets, but didn't account for names with spaces.

sArr = line.split(("[\\s,(|)]+"));  

How could I match only commas, or commas with a whitespace, so that "Sao Paulo" doesn't turn into "Sao","Paulo"?

🌐
BeginnersBook
beginnersbook.com › 2022 › 09 › java-code-to-split-string-by-comma
Java Code to Split String by Comma
September 22, 2022 - In this program, the string is entered by user, which we will capture using Scanner class. import java.util.Scanner; public class JavaExample { public static void main(String[] args) { String str; //getting the string from user System.out.println("Enter a string that contains commas: "); Scanner scan = new Scanner(System.in); str = scan.nextLine(); String[] strArray = str.split(","); System.out.println("Split result: "); for(String s: strArray){ System.out.println(s); } } }
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › split-string-with-comma-in-java
Split String with Comma (,) in Java
March 11, 2026 - Let’s say the following is our string. To split a string with comma, use the split() method in Java. The following is the complete example.
🌐
Briebug
blog.briebug.com › home › articles › using the java string.split() method
Using the Java String.split() Method | Briebug
October 13, 2022 - ***** Split on digits regex example · So let’s explain what is going on here. First, we initialize a String variable, ourString to “This,is,a,comma,separated,string”. Next, is where the magic happens. We declare a String array, ourStringSplitArray and set it’s value by calling the split() method on ourString.
🌐
Coderanch
coderanch.com › t › 451685 › java › split-String-array-comma-seperated
How to split String array which has comma seperated values (Beginning Java forum at Coderanch)
June 29, 2009 - You could use java.util.Scanner. This will iterate through each number as it finds them, as opposed to String.split(), which will hold all the numbers in memory in one giant array. ... Another option... instead of using the regex split() method, you could use the regex find() method. With find(), you can defined the token to include, up to 999 values separated by commas -- and hence, no need to split and remerge 999 items.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java examples › split a comma-separated string in java
Split a comma-separated String in Java - Apps Developer Blog
September 6, 2022 - 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); } } ... // 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));
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › split
Java String split() - Split String | Vultr Docs
December 17, 2024 - By using the trim() method before splitting, lead and trailing spaces are removed, thus avoiding any empty string results due to spaces at the start or end of the string. Get acquainted with the typical format of a CSV (Comma-Separated Values) file.
🌐
Quora
quora.com › How-do-I-split-a-string-in-Java-and-display-it-with-commas
How to split a string in Java and display it with commas - Quora
String.join(delimiter, Iterable/array) is the simplest way to print with commas. ... To split a string in Java and display the parts separated by commas, follow these concise patterns depending on the input and desired behavior.
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... String myStr = "Split a string by spaces, and also punctuation."; String regex = "[,\\.\\s]"; String[] myArray = myStr.split(regex); for (String s : myArray) { System.out.println(s); }
Top answer
1 of 4
22

I've written a quick and dirty benchmark test for this. It compares 7 different methods, some of which require specific knowledge of the data being split.

For basic general purpose splitting, Guava Splitter is 3.5x faster than String#split() and I'd recommend using that. Stringtokenizer is slightly faster than that and splitting yourself with indexOf is twice as fast as again.

For the code and more info see https://web.archive.org/web/20210613074234/http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance (original link is dead and corresponding site does not appear to exist anymore)

2 of 4
6

As @Tom writes, an indexOf type approach is faster than String.split(), since the latter deals with regular expressions and has a lot of extra overhead for them.

However, one algorithm change that might give you a super speedup. Assuming that this Comparator is going to be used to sort your ~100,000 Strings, do not write the Comparator<String>. Because, in the course of your sort, the same String will likely be compared multiple times, so you will split it multiple times, etc...

Split all the Strings once into String[]s, and have a Comparator<String[]> sort the String[]. Then, at the end, you can combine them all together.

Alternatively, you could also use a Map to cache the String -> String[] or vice versa. e.g. (sketchy) Also note, you are trading memory for speed, hope you have lotsa RAM

HashMap<String, String[]> cache = new HashMap();

int compare(String s1, String s2) {
   String[] cached1 = cache.get(s1);
   if (cached1  == null) {
      cached1 = mySuperSplitter(s1):
      cache.put(s1, cached1);
   }
   String[] cached2 = cache.get(s2);
   if (cached2  == null) {
      cached2 = mySuperSplitter(s2):
      cache.put(s2, cached2);
   }

   return compareAsArrays(cached1, cached2);  // real comparison done here
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-a-string-in-java-with-delimiter
How to Split a String in Java with Delimiter? - GeeksforGeeks
July 23, 2025 - Here is a simple program to split a string with a comma delimiter in Java using the split() method:
🌐
Reddit
reddit.com › r/learnpython › splitting strings on comma with embedded commas
r/learnpython on Reddit: Splitting Strings on Comma With Embedded Commas
December 8, 2019 -

I have a sting which I want to split on comma. However, some of the terms that have been joined to create the string have a comma within them which I do not wish to split on. As an example, the string format I am dealing with follows:

'Cat1,Cat2, Big,Cat3, Cat4, Small,Cat5' -> 'Cat1', 'Cat2, Big', 'Cat3', 'Cat4, Small', 'Cat5'.

I tried using regex to separate where we have a comma followed by an uppercase character but could not seem to get it working. A standard comma when splitting would incorrectly split Cat3, Big into 'Cat3', 'Big'. Does anyone have an ideas to overcome this issue?

🌐
UiPath Community
forum.uipath.com › help
Splitting by comma, ignore "" - Help - UiPath Community Forum
August 8, 2019 - Hi, I am splitting data by comma, data is FirstName,LastName,Mr,T,“Note: , First Line,this is contact detail of T”, test account,test But I want that data in doube quotes having comma inside should be ignored. so i want 1 item in array from above data like - Note: , First Line,this is contact ...
🌐
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 ...
🌐
Quora
quora.com › How-can-I-split-the-string-into-in-Java
How to split the string '[] {() <>} []' into ' [,], {, (, ...' in Java - Quora
Answer (1 of 11): If you only want to print the characters in the original string separated by comma, then one possible way is to use the String.chars() method: [code]System.out.println("[]{() }[]" .chars() .mapToObj(Character::toString) .collect(Collectors.joining(","))...