Return Value 戻り値
An array of subsequences, split from this sequence’s elements. このシーケンスの要素を分割した、下位シーケンスいくらかからなる配列。
Availability 有効性
Technology
func split(maxSplits: Int
= Int.max, omittingEmptySubsequences: Bool
= true, whereSeparator isSeparator: (Self.Element) throws -> Bool
) rethrows -> [ArraySlice
<Self.Element>]
An array of subsequences, split from this sequence’s elements. このシーケンスの要素を分割した、下位シーケンスいくらかからなる配列。
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. max
must be greater than or equal to zero. The default value is Int
.
シーケンスを分割する回数の最大限度、または返される下位シーケンスの数より1つ少ないもの。max
の下位シーケンスが返されるならば、最後のものは、残りの要素を含んでいる元シーケンスの後ろ部分です。max
は、ゼロより大きいか等しくなければなりません。省略時の値は、Int
です。
omittingEmptySubsequences
If false
, an empty subsequence is returned in the result for each pair of consecutive elements satisfying the is
predicate and for each element at the start or end of the sequence satisfying the is
predicate. If true
, only nonempty subsequences are returned. The default value is true
.
isSeparator
A closure that returns true
if its argument should be used to split the sequence; otherwise, false
.
The following examples show the effects of the max
and omitting
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.
以下の例は、max
とomitting
パラメータの効果を、空白にマッチするクロージャを使って文字列を分割する場合で示します。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 max
parameter, so the original string is split just once, into two new strings.
2番目の例は1
をmax
パラメータに渡します、それで元の文字列は一度だけ分割されて、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 allow
parameter, so the returned array contains empty strings where spaces were repeated.
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.