Returns the longest possible subsequences of the sequence, in order, around elements equal to the given element.
SDK
- Xcode 11.0+
Framework
- Swift Standard Library
Declaration
Parameters
separator
The element that should be split upon.
maxSplits
The maximum number of times to split the sequence, or one less than the number of subsequences to return. If
max
subsequences are returned, the last one is a suffix of the original sequence containing the remaining elements.Splits + 1 max
must be greater than or equal to zero. The default value isSplits Int
..max omittingEmptySubsequences
If
false
, an empty subsequence is returned in the result for each consecutive pair ofseparator
elements in the sequence and for each instance ofseparator
at the start or end of the sequence. Iftrue
, only nonempty subsequences are returned. The default value istrue
.
Return Value
An array of subsequences, split from this sequence’s elements.
Discussion
The resulting array consists of at most max
subsequences. Elements that are used to split the sequence are not returned as part of any subsequence.
The following examples show the effects of the max
and omitting
parameters when splitting a string at each space character (” “). The first use of split
returns each word that was originally separated by one or more spaces.
The second example passes 1
for the max
parameter, so the original string is split just once, into two new strings.
The final example passes false
for the omitting
parameter, so the returned array contains empty strings where spaces were repeated.
Complexity: O(n), where n is the length of the sequence.