Swift Programming
String Manipulation
Array Operations
Coding Tutorial
iOS Development

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.

Practice algorithms

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.

swift
1let text = "apple,banana,cherry"
2let parts = text.split(separator: ",")
3
4print(parts)
5print(type(of: parts[0]))

This prints an array of Substring values. If you need full String values, map them:

swift
let strings = text.split(separator: ",").map(String.init)
print(strings)

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.

swift
1let csvLine = "red,,blue,"
2
3let defaultSplit = csvLine.split(separator: ",")
4print(defaultSplit)
5
6let keepingEmpties = csvLine.split(
7    separator: ",",
8    omittingEmptySubsequences: false
9)
10print(keepingEmpties)

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:

swift
1import Foundation
2
3let text = "apple, banana;orange pear"
4let rawParts = text.components(separatedBy: CharacterSet(charactersIn: ",; ").union(.whitespacesAndNewlines))
5let parts = rawParts.filter { !$0.isEmpty }
6
7print(parts)

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:

swift
1let input = "key=value=rest"
2let pieces = input.split(separator: "=", maxSplits: 1)
3
4print(pieces)

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 String results 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 split for most simple delimiter-based splitting in Swift.
  • 'split returns Substring values and omits empty fields by default.'
  • Use map(String.init) when you need full String results.
  • Use Foundation components(separatedBy:) for CharacterSet-based splitting or immediate String arrays.
  • Be explicit about empty-field handling when parsing structured input.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.