How can I convert a comma-separated string to an array?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The basic answer is to split the string on commas. The real work starts after that, because production input often contains spaces, empty items, quoted values, or values that should be converted into numbers instead of left as raw text.
The Simplest Case
If the input is a plain comma-separated list like apple,banana,orange, most languages provide a built-in split operation.
JavaScript example:
Python example:
Both produce three strings.
Trim Whitespace Immediately
Real input usually contains spaces after commas. If you do not trim them, the array contains values like " banana" instead of "banana".
This should be the default approach for user-facing input.
Handle Empty Entries Deliberately
Input such as apple,,orange, creates empty strings. Sometimes that is valid, and sometimes it should be filtered out.
Whether you keep or remove empty values depends on the meaning of the data.
Convert Types After Splitting
Many comma-separated strings really represent numbers or IDs. Do not keep them as strings if the rest of the program expects numeric values.
Validate the conversion if the input is untrusted.
CSV Is Not the Same as "Split on Comma"
This is the biggest conceptual pitfall. A CSV line can contain quoted commas, escaped quotes, and embedded newlines. For example, "New York, NY" is one field, not two.
If the input is real CSV, use a CSV parser instead of plain string splitting.
Python example:
JavaScript in Node.js usually relies on a CSV library for this case.
A Small Reusable Helper
If you do this often, wrap the behavior into one function so trimming and filtering stay consistent.
This is safer than repeating slightly different parsing logic throughout the codebase.
Language-Agnostic Rule of Thumb
For a simple list, the workflow is:
- split on commas
- trim each item
- optionally remove empties
- optionally convert types
- switch to a CSV parser if quoting rules matter
That sequence covers most real applications.
Common Pitfalls
A common mistake is assuming split(',') is a full CSV parser. It is not.
Another mistake is forgetting to trim whitespace, which leads to subtle mismatches later when comparing values.
Developers also often ignore empty fields until those empties propagate into APIs, database inserts, or validation logic.
Summary
- Use the language's split function for simple comma-separated input.
- Trim whitespace after splitting in almost all user-facing cases.
- Decide explicitly whether empty items should be kept or removed.
- Convert to numeric or other target types after parsing.
- Use a real CSV parser when quotes or embedded commas are possible.
Related reading
- How can I convert a dictionary into a list of tuples?
- How can I convert comma separated string into a Listint
- How can I convert each item in the list to string, for the purpose of joining them?
- How can I convert int to Integer in Java?
- How can I convert JSON to a HashMap using Gson?
- How can I convert List<Integer> to int[] in Java?
- How can I count the unique numbers in an array without rearranging the array elements?
- How can I create a generic array in Java?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.