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

split(separator:maxSplits:omittingEmptySubsequences:)

Returns the longest possible subsequences of the sequence, in order, around elements equal to the given element. 与えられた要素と等しい要素を避けるようにして、このシーケンスの最も長くなりうる下位シーケンスを、順番に返します。

Declaration 宣言

func split(separator: Base.Element, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true) -> [ArraySlice<Base.Element>]
Available when Base.Element conforms to Equatable. Base.ElementEquatableに準拠する時に利用可能です。

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 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 consecutive pair of separator elements in the sequence and for each instance of separator at the start or end of the sequence. If true, only nonempty subsequences are returned. The default value is true. falseならば、このシーケンス中の連続するseparator要素の対それぞれに、そしてシーケンスの始めと終わりのseparatorインスタンスそれぞれに、1つの空の下位シーケンスが結果において返されます。trueならば、空でない下位シーケンスだけが返されます。省略時の値は、trueです。

Return Value 戻り値

An array of subsequences, split from this sequence’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 at each space character (” “). 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(separator: " ")
          .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(separator: " ", maxSplits: 1)
          .map(String.init))
// 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(separator: " ", omittingEmptySubsequences: false)
          .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はシーケンスの長さです。