Software, Tech & Coding simplified.

Swift Split String to an Array

To split a string to an array in Swift by a character, use the String.split(separator:) function.

For example, let’s split a string by commas:

let people = "Alice,Bob,David,Charlie"
let peopleList = people.split(separator: ",")

print(peopleList)

Output:

["Alice", "Bob", "David", "Charlie"]

However, this requires that the separator is a singular character, not a string. To split a string to an array by a separator string, use String.components(separatedBy:) function.

For example, let’s split a string by three dashes “—“:

let people = "Alice---Bob---David---Charlie"
let peopleList = people.components(separatedBy: "---")

print(peopleList)

Output:

["Alice", "Bob", "David", "Charlie"]

In this guide, you are going to learn how to use both String.split() and String.components() functions in detail. You are also going to see a problem both functions have.

The String.split() Function

To split a string into an array in Swift, use the String.split() function.

Example

For example, to split a string by spaces:

var sentence = "This is a test"
var parts = sentence.split(separator: " ")

print(parts)

Output:

["This", "is", "a", "test"]

Full Syntax

The full syntax for using the String.split() function is:

String.split(separator: Character, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true)

Where:

  • separator specifies the separator type based on which to split the string.
  • maxSplits specifies the number of splits that will be made.
  • omittingEmptySubsequences indicates whether consecutive separators are included in the result or not.

Let’s go through using each of these arguments in more detail.

separator

The separator is a string that specifies based on what you want to split the string.

For example, let’s split a string of comma-separated values by calling the split() function with a comma as a separator:

var people = "Alice,Bob,David,Charlie"
var peopleList = people.split(separator: ",")

print(peopleList)

Output:

["Alice", "Bob", "David", "Charlie"]

Notice that the separator has to be a Character, that is, one letter. It is thus not possible to use a String of multiple characters as a separator.

maxSplits

The maxSplits parameter is an optional parameter. It determines how many strings you want to separate from the original string (based on a separator). After maxSplits amount of splits, the rest of the string is added as a whole into the result array.

For example, if you have a string of 10 space-delimited numbers and you only want to grab the first 5 of them into an array, use maxSplits of 5.

For example:

var people = "1 2 3 4 5 6 7 8 9 10"
var peopleList = people.split(separator: " ", maxSplits: 5)

print(peopleList)

Output:

["1", "2", "3", "4", "5", "6 7 8 9 10"]

As you can see, the first 5 numbers are added as separate elements in the result array. The rest of the numbers are all in one string as the last element of the array.

omittingEmptySubsequences

The omittingEmptySubsequences parameter is an optional parameter. It is a boolean value that specifies whether to include an empty string for each consecutive separator character in the original string. This probably rings no bells, so feel free to check the example below.

omittingEmptySubsequences is true by default. In other words, it behaves intuitively and leaves the empty strings out from the result.

Example. If there are 3 consecutive spaces in a string and you split the string with space as a separator, you probably want to leave the extra spaces out from the result. By default, the empty strings are indeed left out:

var people = "This   is   a    test"
var peopleList = people.split(separator: " ")

print(peopleList)

Output:

["This", "is", "a", "test"]

But if you want to include the consecutive spaces as empty strings in the result, set omittingEmptySubsequences false.

var people = "This   is   a   test"
var peopleList = people.split(separator: " ", omittingEmptySubsequences: false)

print(peopleList)

Now each consecutive space is present as an empty string in the result array:

["This", "", "", "is", "", "", "a", "", "", "test"]
Do not omit empty subsequences in Swift

By the way, no matter what the separator character is, it will always be represented by an empty string in the result array (when omittingEmptySubsequences is false):

var people = "This,,,is,,,a,,,test"
var peopleList = people.split(separator: ",", omittingEmptySubsequences: false)

print(peopleList)

Output:

["This", "", "", "is", "", "", "a", "", "", "test"]

The Problem with String.split() Function

So far you have learned how to use String.split() function to split a string to an array. However, this function can only use a character as a separator. If you want to use a string separator, call the String.components(separatedBy:) function instead.

String.components() in Swift

To split a string into an array by using a separator string, call the String.components(separatedBy:) function.

Here the argument separatedBy can be either one of the following:

  • A separator string. This allows to split the string by a string.
  • A CharacterSet. This allows to split the string by any of the characters specified in the CharacterSet.

Let’s take a look at an example of both.

Split a String by a String

Let’s split a string using a string as a separator. To do this, call the String.components(separatedBy:) function with a string argument.

For example:

let people = "Alice---Bob---David---Charlie"
let peopleList = people.components(separatedBy: "---")

print(peopleList)

Output:

["Alice", "Bob", "David", "Charlie"]

Split a String by Any Character in a CharacterSet

Let’s split a string separated by any character in a group of characters.

Example. Split a string that separates names with either “,”, “;”, or “-“.

This is possible by defining a CharacterSet that which is a group of characters that separate a string. You can then pass the CharacterSet into the String.components(separatedBy:) function call:

import Foundation

let people = "Alice;Bob,David-Charlie"
let separators = CharacterSet(charactersIn: ",;-")
let peopleList = people.components(separatedBy: separators)

print(peopleList)

Output:

["Alice", "Bob", "David", "Charlie"]

The problem with String.components() Function

We started this guide by using the String.split() function. Because it only allows using a character as a separator, you may want to use String.components() function. It lets you split a string using a string as a separator.

However, String.components() function also has a problem with white spaces that can be solved by using the String.split() function.

For example, let’s split a string that has multiple consecutive spaces.

First, let’s use the String.components() function:

var people = "This     is a   test"
var peopleList = people.components(separatedBy: " ")

print(peopleList)

The resulting array has empty strings that represent the spaces separated by spaces:

["This", "", "", "", "", "is", "a", "", "", "test"]

Usually, this is not what you want. If this is a problem for you, use the String.split() function that leaves the empty strings out.

var people = "This     is a   test"
var peopleList = people.split(separator: " ")

print(peopleList)

Output:

["This", "is", "a", "test"]

Conclusion

Today you learned how to split a string to an array in Swift.

To recap, you can split a string by a single character using the String.split(separator:) function. To split using a string as a separator, use the String.components(separatedBy:) function.

Thanks for reading.

Happy coding!

Further Reading

50 Swift Interview Questions

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email