Instance Method インスタンスメソッド

split(maxSplits:omittingEmptySubsequences:whereSeparator:)

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate. 与えられた述部を満たす要素を含んでいない、このコレクションの最も長くなりうる下位シーケンスそれらを順序どおりに返します。

Declaration 宣言

func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Base.Element) throws -> Bool) rethrows -> [Slice<ReversedCollection<Base>>]

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 is Int.max. コレクションを分割する回数の最大限度、または返される下位シーケンスの数より1つ少ないもの。maxSplits + 1の下位シーケンスが返されるならば、最後のものは、残りの要素を含んでいる元コレクションの後ろ部分です。maxSplitsは、ゼロより大きいか等しくなければなりません。省略時の値はInt.maxです。

omittingEmptySubsequences

If false, an empty subsequence is returned in the result for each pair of consecutive elements satisfying the isSeparator predicate and for each element at the start or end of the collection satisfying the isSeparator predicate. The default value is true. falseならば、結果においてisSeparator述部を満たす隣接する要素のペアそれぞれに対しておよびisSeparator述部を満たすコレクションの始まりまたは終わりでの要素それぞれに対して1つの空の下位シーケンスが返されます。省略時の値は、trueです。

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. あるクロージャ、それは1つの要素を引数として取り、コレクションがその要素で分割されるかどうかを指し示すブール値を返します。

Return Value 戻り値

An array of subsequences, split from this collection’s elements. このコレクションの要素を分割した、下位シーケンスいくらかからなる配列。

Discussion 解説

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. 結果の配列は、多くともmaxSplits + 1の下位シーケンスから成ります。シーケンスを分割するのに使われる要素らは、どれかの下位シーケンスの一部として返されません。

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. 以下の例は、maxSplitsomittingEmptySubsequencesパラメータの効果を、空白にマッチするクロージャを使って文字列を分割する場合で示します。splitの最初の利用は、各単語を返します、それはもともと1つ以上の空白で隔てられています。


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. 2番目の例は1maxSplitsパラメータに渡します、それで元の文字列は一度だけ分割されて、2つの新しい文字列になります。


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. 最後の例はfalseをomittingEmptySubsequencesパラメータに渡します、なので返される配列は空白が繰り返されたところで空の文字列を含みます。


print(line.split(omittingEmptySubsequences: false, whereSeparator: { $0 == " " }))
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"

Complexity: O(n), where n is the length of the collection. 計算量:O(n)、ここでnはコレクションの長さです。