Split a String into an array in Swift?
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 string into an array is one of the most common text-processing tasks in Swift. The right API depends on what you want to split on, whether you want empty fields preserved, and whether you want the result as Substring values or full String values.
split Versus components
Swift gives you two main styles:
- '
split, which is part of the standard library' - '
components(separatedBy:), which comes from Foundation'
split is usually the first choice for simple delimiter-based work because it is lightweight and returns Substring values without eagerly copying each piece.
This prints an array of Substring values. If you need full String values, map them:
Preserving Empty Fields
One important behavior of split is that it omits empty subsequences by default. That is good for free-form text, but wrong for CSV-like data where empty columns matter.
The second call preserves the empty fields. That distinction matters a lot in data import code.
Splitting on More Than One Delimiter
If you need to split on a set of characters such as commas, semicolons, or whitespace, Foundation can be convenient:
This is a practical approach when delimiters are character-based rather than a single fixed separator string.
Limiting the Number of Splits
Sometimes you only want to split once or twice. split supports that with maxSplits:
This is useful when parsing formats where only the first delimiter matters.
When to Choose Which API
Use split when:
- the separator is a single character
- you want efficient standard-library behavior
- you are comfortable working with
Substring
Use components(separatedBy:) when:
- you already depend on Foundation
- you want
Stringresults immediately - you need
CharacterSet-based splitting
Neither is universally better. The key is to pick the one that matches the data shape you actually have.
If performance matters, prefer staying in the standard library unless you specifically need Foundation behavior. The difference is rarely dramatic for short strings, but it is still a useful habit when splitting happens in tight parsing loops. It also keeps dependency boundaries simpler in pure Swift utility code. That matters in shared libraries and command-line tools.
Common Pitfalls
The biggest mistake is forgetting that split returns Substring, not String. That is fine for local processing, but convert to String if the results will be stored long-term.
Another mistake is relying on default split behavior when empty fields are significant. If the input format can contain missing values, set omittingEmptySubsequences explicitly.
A third issue is using simple delimiter splitting for real CSV parsing. Quoted fields and escaped separators need a dedicated parser, not a plain string split.
Summary
- Use
splitfor most simple delimiter-based splitting in Swift. - '
splitreturnsSubstringvalues and omits empty fields by default.' - Use
map(String.init)when you need fullStringresults. - Use Foundation
components(separatedBy:)forCharacterSet-based splitting or immediateStringarrays. - Be explicit about empty-field handling when parsing structured input.
Related reading
- Split a String into an array in Swift?
- Split array into chunks
- Split list into smaller lists split in half
- Split tensor into training and test sets
- Split an NSString to access one particular piece
- Spring Boot with Kotlin - Value annotation not working as expected
- Split vector into balanced list balancing sum of list elements
- Splitting a list into N parts of approximately equal length

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.