Introduction: Why String Splitting is a Fundamental Skill Every Swift Developer Must Know
\n\nImagine 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\nIn 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.
1. The Classic Method: components(separatedBy:)
\n\nThis 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.
import Foundation\n\nlet text = "apple,banana,orange,kiwi"\nlet fruits = text.components(separatedBy: ",")\nprint(fruits) // ["apple", "banana", "orange", "kiwi"]\n\nImportant 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
let data = "a,,b,c"\nlet parts = data.components(separatedBy: ",")\nprint(parts) // ["a", "", "b", "c"]\n\nThis 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\n2. The Modern Method: split(separator:maxSplits:omittingEmptySubsequences:)
\n\nStarting 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.
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\nParameters of the split method:
\n- \n
separator— the delimiter character (Character). \n maxSplits— the maximum number of splits (default isInt.max). \n omittingEmptySubsequences— whether to remove empty substrings (default istrue). \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\nIf 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).
let result: [String] = "a,b,c".split(separator: ",").map(String.init)\nprint(result) // ["a", "b", "c"]\n