Returns the longest possible subsequences of the sequence, in order, that don’t contain elements satisfying the given predicate. Elements that are used to split the sequence are not returned as part of any subsequence.
SDK
- Xcode 10.2+
Framework
- Swift Standard Library
Declaration
func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Self.Element) throws -> Bool) rethrows -> [ArraySlice <Self.Element>]
Parameters
maxSplitsThe maximum number of times to split the sequence, or one less than the number of subsequences to return. If
maxsubsequences are returned, the last one is a suffix of the original sequence containing the remaining elements.Splits + 1 maxmust be greater than or equal to zero. The default value isSplits Int..max omittingEmptySubsequencesIf
false, an empty subsequence is returned in the result for each pair of consecutive elements satisfying theispredicate and for each element at the start or end of the sequence satisfying theSeparator ispredicate. IfSeparator true, only nonempty subsequences are returned. The default value istrue.isSeparatorA closure that returns
trueif its argument should be used to split the sequence; otherwise,false.
Return Value
An array of subsequences, split from this sequence’s elements.
Discussion
The following examples show the effects of the max and omitting parameters when splitting a string using a closure that matches spaces. The first use of split returns each word that was originally separated by one or more spaces.
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(whereSeparator: { $0 == " " })
.map(String.init))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
The second example passes 1 for the max parameter, so the original string is split just once, into two new strings.
print(
line.split(maxSplits: 1, whereSeparator: { $0 == " " })
.map(String.init))
// Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
The final example passes true for the allow parameter, so the returned array contains empty strings where spaces were repeated.
print(
line.split(
omittingEmptySubsequences: false,
whereSeparator: { $0 == " " }
).map(String.init))
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
Complexity: O(n), where n is the length of the sequence.