Instance Methodswift 6.0.1Swift
split(maxSplits:omittingEmptySubsequences:whereSeparator:)
Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.
func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Self.Element) throws -> Bool) rethrows -> [Self.SubSequence]
Parameters
- maxSplits
The maximum number of times to split the collection, or one less than the number of subsequences to return. If
maxSplits + 1
subsequences are returned, the last one is a suffix of the original collection containing the remaining elements.maxSplits
must be greater than or equal to zero. The default value isInt.max
.- omittingEmptySubsequences
If
false
, an empty subsequence is returned in the result for each pair of consecutive elements satisfying theisSeparator
predicate and for each element at the start or end of the collection satisfying theisSeparator
predicate. The default value istrue
.- isSeparator
A closure that takes an element as an argument and returns a Boolean value indicating whether the collection should be split at that element.
Returns
An array of subsequences, split from this collection’s elements.
The resulting array consists of at most maxSplits + 1
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 maxSplits
and omittingEmptySubsequences
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 == " " }))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
The second example passes 1
for the maxSplits
parameter, so the original string is split just once, into two new strings.
print(line.split(maxSplits: 1, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
The final example passes false
for the omittingEmptySubsequences
parameter, so the returned array contains empty strings where spaces were repeated.
print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
Other members in extension
Typealiases
Show obsolete interfaces (1)
Hide obsolete interfaces
Instance members
var count: Int
The number of elements in the collection.
var first: Self.Element?
The first element of the collection.
var isEmpty: Bool
A Boolean value indicating whether the collection is empty.
var underestimatedCount: Int
A value less than or equal to the number of elements in the collection.
subscript<R>(R
) -> Self.SubSequence Accesses the contiguous subrange of the collection’s elements specified by a range expression.
subscript((UnboundedRange_) -> ()
) -> Self.SubSequence subscript(RangeSet<Self.Index>
) -> DiscontiguousSlice<Self> Accesses a view of this collection with the elements at the given indices.
func distance(from: Self.Index, to: Self.Index
) -> Int Returns the distance between two indices.
func drop(while: (Self.Element) throws -> Bool
) rethrows -> Self.SubSequence Returns a subsequence by skipping elements while
predicate
returnstrue
and returning the remaining elements.func dropFirst(Int
) -> Self.SubSequence Returns a subsequence containing all but the given number of initial elements.
func dropLast(Int
) -> Self.SubSequence Returns a subsequence containing all but the specified number of final elements.
func firstIndex(where: (Self.Element) throws -> Bool
) rethrows -> Self.Index? Returns the first index in which an element of the collection satisfies the given predicate.
func formIndex(inout Self.Index, offsetBy: Int
) Offsets the given index by the specified distance.
func formIndex(inout Self.Index, offsetBy: Int, limitedBy: Self.Index
) -> Bool Offsets the given index by the specified distance, or so that it equals the given limiting index.
func formIndex(after: inout Self.Index
) Replaces the given index with its successor.
func index(Self.Index, offsetBy: Int
) -> Self.Index Returns an index that is the specified distance from the given index.
func index(Self.Index, offsetBy: Int, limitedBy: Self.Index
) -> Self.Index? Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
func indices(where: (Self.Element) throws -> Bool
) rethrows -> RangeSet<Self.Index> Returns the indices of all the elements that match the given predicate.
func map<T, E>((Self.Element)
throws Returns an array containing the results of mapping the given closure over the sequence’s elements.
func prefix(Int
) -> Self.SubSequence Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.
func prefix(through: Self.Index
) -> Self.SubSequence Returns a subsequence from the start of the collection through the specified position.
func prefix(upTo: Self.Index
) -> Self.SubSequence Returns a subsequence from the start of the collection up to, but not including, the specified position.
func prefix(while: (Self.Element) throws -> Bool
) rethrows -> Self.SubSequence Returns a subsequence containing the initial elements until
predicate
returnsfalse
and skipping the remaining elements.func randomElement(
) -> Self.Element? Returns a random element of the collection.
func randomElement<T>(using: inout T
) -> Self.Element? Returns a random element of the collection, using the given generator as a source for randomness.
func removingSubranges(RangeSet<Self.Index>
) -> DiscontiguousSlice<Self> Returns a collection of the elements in this collection that are not represented by the given range set.
func suffix(Int
) -> Self.SubSequence Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
func suffix(from: Self.Index
) -> Self.SubSequence Returns a subsequence from the specified position to the end of the collection.
Show obsolete interfaces (6)
Hide obsolete interfaces
func distance<T>(from: Self.Index, to: Self.Index
) -> T func flatMap((Self.Element) throws -> String?
) rethrows -> [String] func formIndex<T>(inout Self.Index, offsetBy: T
) func formIndex<T>(inout Self.Index, offsetBy: T, limitedBy: Self.Index
) -> Bool func index<T>(Self.Index, offsetBy: T
) -> Self.Index func index<T>(Self.Index, offsetBy: T, limitedBy: Self.Index
) -> Self.Index?