How to split a delimited string to a ListString
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
Splitting a delimited string into a List<String> is simple in Java until edge cases appear, such as empty tokens, extra spaces, or delimiters that are regex metacharacters. A robust approach should normalize values and make delimiter behavior explicit. This guide shows practical patterns you can run directly in a Java project.
Basic Split to List<String>
For plain delimiters such as comma, use String.split and wrap with Arrays.asList or streams.
This works for clean input, but it keeps spaces and may drop trailing empty values depending on options.
Trim Tokens and Remove Empty Entries
Most real inputs need cleanup before use.
This pattern removes accidental blank tokens and gives predictable output for user-entered data.
Keep Trailing Empty Tokens When Needed
By default, split drops trailing empty tokens. If position matters, pass a negative limit.
Use this when tokens map to fixed columns or configuration slots.
Escape Regex Delimiters Correctly
split treats the delimiter as a regex pattern. Characters such as dot or pipe must be escaped.
If delimiters are dynamic, use Pattern.quote to avoid accidental regex behavior.
When Input Is CSV, Use a CSV Parser
If values may contain quoted commas, plain split(",") is not sufficient. Use a CSV library.
CSV parsing avoids subtle bugs when user data includes delimiters inside quoted fields.
Return Immutable Lists for Safer APIs
If a split helper is shared across services, returning an immutable list prevents accidental mutation by callers. This is useful when tokens represent configuration values that should not change after parsing.
You can also wrap parsed output with List.copyOf(parsed) before returning it from utility methods.
Common Pitfalls
- Forgetting that
splituses regex rules, not literal delimiter matching. - Losing trailing empty values because split limit was not set.
- Not trimming tokens before validation or comparison.
- Using plain split for CSV-like data that contains quoted delimiters.
- Returning mutable lists from utility methods when callers expect immutability.
Summary
- Start with
splitfor clean simple input. - Add trim and empty-token filtering for user-entered strings.
- Use
split(delimiter, -1)when positional empties must be preserved. - Escape regex delimiters or use
Pattern.quotefor dynamic delimiters. - Switch to a CSV parser when quoting rules are part of the format.
Related reading
- How to split a string into an array in Bash?
- How to split data on balanced training set and test set on sklearn
- How to store a hash table in a file?
- How to store arrays in MySQL?
- How to split a String by space
- How to start @KafkaListener based on start flag
- How to sum all the values in a dictionary?
- How to switch position of two items in a Python list?

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.