The split() method breaks up a string at the specified separator and returns an array of strings.
Example
split() Syntax
The syntax of split() is:
string.split(separator: Character, maxSplits: Int, ommittingEmptySequence: Bool)
Here, string is an object of the String class.
split() Parameters
The split() method can take three parameters:
- separator - Delimiter at which splits occur.
- maxSplits (optional) - Maximum number of splits. If not provided, there is no limit on the number of splits.
- omittingEmptySubsequences (optional) - specifies whether to omit empty string elements or to include them
Note: If maxSplits is specified, the array will have a maximum of maxSplits + 1 items.
split() Return Value
- returns an array of substrings
Example 1: Swift split()
Output
["Swift is awesome", " Swift is fun"] ["Swift", "is", "awesome. Swift is fun."]
Example 2: split() with omittingEmptySubsequences Parameter
Output
["Swift", "is", "a fun programming language"] ["Swift", "", "is a fun programming language"]