How to Split a String into an Array in Swift: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why String Splitting is a Fundamental Skill Every Swift Developer Must Know

\n\n

Imagine this scenario: you are parsing a CSV file, processing user input from a text field, or parsing a JSON-like string. What do you do? That's right — you split the string into an array. The question "Split a String into an array in Swift?" on Stack Overflow has garnered over 20,000 views and has become one of the most popular among beginner iOS developers. Why? Because without this skill, it's impossible to write any serious project in Swift.

\n\n

In this article, we will cover all possible ways to split a string into an array in Swift: from the classic components(separatedBy:) to the modern split(separator:) and clever techniques with regular expressions. You will learn which method to choose depending on the task and get ready-to-use code examples that you can immediately insert into your Xcode project.

\n\n

1. The Classic Method: components(separatedBy:)

\n\n

This method comes to us from Objective-C and is still actively used. It is simple, clear, and works like clockwork. components(separatedBy:) returns an array of strings [String], separated by the specified delimiter.

\n\n
import Foundation\n\nlet text = "apple,banana,orange,kiwi"\nlet fruits = text.components(separatedBy: ",")\nprint(fruits) // ["apple", "banana", "orange", "kiwi"]
\n\n

Important nuances of the components(separatedBy:) method:

\n
    \n
  • The separator is a string. You can pass any String, even with multiple characters: "::", " - ".
  • \n
  • Empty elements are preserved. If there is nothing between separators, an empty string will appear in the array.
  • \n
  • Requires import Foundation. The method is unavailable without the import.
  • \n
\n\n
let data = "a,,b,c"\nlet parts = data.components(separatedBy: ",")\nprint(parts) // ["a", "", "b", "c"]
\n\n

This method is ideal when you need to quickly and easily split a string into an array in Swift without any extra fuss. But remember: it does not automatically remove empty substrings.

\n\n

2. The Modern Method: split(separator:maxSplits:omittingEmptySubsequences:)

\n\n

Starting with Swift 2.0, we have the native split() method. It is more flexible and "Swifty". Unlike components, split returns an array of Substring, not String. This is done for performance optimization — substrings reference the memory of the original string.

\n\n
let sentence = "The quick brown fox jumps over the lazy dog"\nlet words = sentence.split(separator: " ")\nprint(words) // ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
\n\n

Parameters of the split method:

\n
    \n
  • separator — the delimiter character (Character).
  • \n
  • maxSplits — the maximum number of splits (default is Int.max).
  • \n
  • omittingEmptySubsequences — whether to remove empty substrings (default is true).
  • \n
\n\n
let csvLine = "1,2,,4,5"\nlet withEmpties = csvLine.split(separator: ",", omittingEmptySubsequences: false)\nprint(withEmpties) // ["1", "2", "", "4", "5"]\n\nlet withoutEmpties = csvLine.split(separator: ",", omittingEmptySubsequences: true)\nprint(withoutEmpties) // ["1", "2", "4", "5"]
\n\n

If you need to split a string into an array of Substring in Swift and want to control empty elements — choose split. And to get [String], simply add .map(String.init).

\n\n
let result: [String] = "a,b,c".split(separator: ",").map(String.init)\nprint(result) // ["a", "b", "c"]
\n

Blogs

Book Recommendations