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

split(maxSplits:omittingEmptySubsequences:whereSeparator:)

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. 与えられた述部を満たす要素を含んでいない、このシーケンスの最も長くなりうる下位シーケンスそれらを順序どおりに返します。シーケンスを分割するのに使われる要素らは、どれかの下位シーケンスの一部として返されません。

Declaration 宣言

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

Parameters パラメータ

maxSplits

The maximum number of times to split the sequence, 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 sequence 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 sequence satisfying the isSeparator predicate. If true, only nonempty subsequences are returned. The default value is true. falseならば、結果においてisSeparator述部を満たす隣接する要素のペアそれぞれに対しておよびisSeparator述部を満たすシーケンスの始まりまたは終わりでの要素それぞれに対して1つの空の下位シーケンスが返されます。trueならば、空でない下位シーケンスだけが返されます。省略時の値は、trueです。

isSeparator

A closure that returns true if its argument should be used to split the sequence; otherwise, false. 1つのクロージャ、それはそれの引数が使われてシーケンスを分割するならばtrueを;そうでなければ、falseを返します。

Return Value 戻り値

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

Discussion 解説

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 == " " })
          .map(String.init))
// 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 == " " })
                  .map(String.init))
// Prints "["BLANCHE:", "  I don\'t want realism. I want magic!"]"

The final example passes true for the allowEmptySlices parameter, so the returned array contains empty strings where spaces were repeated. 最後の例はtrueallowEmptySlicesパラメータに渡します、なので返される配列は空白が繰り返されたところで空の文字列を含みます。


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. 計算量:O(n)、ここでnはシーケンスの長さです。