Generic Instance Method 総称体インスタンスメソッド

starts(with:)

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence. シーケンスの最初の要素らが別のシーケンスの中の要素らと同じかどうかを指し示すブール値を返します。

Declaration 宣言

func starts<PossiblePrefix>(with possiblePrefix: PossiblePrefix) -> Bool where PossiblePrefix : Sequence, Base.Element == PossiblePrefix.Element
Available when Base.Element conforms to Equatable. Base.ElementEquatableに準拠する時に利用可能です。

Parameters パラメータ

possiblePrefix

A sequence to compare to this sequence. あるシーケンス、このシーケンスと比べることになります。

Return Value 戻り値

true if the initial elements of the sequence are the same as the elements of possiblePrefix; otherwise, false. If possiblePrefix has no elements, the return value is true. true、もしシーケンスの冒頭の要素がpossiblePrefixの要素と同じならば;そうでなければfalsepossiblePrefixが1つも要素を持たないならば、戻り値はtrueです。

Discussion 解説

This example tests whether one countable range begins with the elements of another countable range. この例は、ある可付番範囲が別の可付番範囲の要素で始まるかどうかテストします。


let a = 1...3
let b = 1...10


print(b.starts(with: a))
// Prints "true"

Passing a sequence with no elements or an empty collection as possiblePrefix always results in true. 1つの要素もないシーケンスまたは空のコレクションをpossiblePrefixとして渡すことは、常にtrueという結果になります。


print(b.starts(with: []))
// Prints "true"

Complexity: O(m), where m is the lesser of the length of the sequence and the length of possiblePrefix. 計算量:O(m)、ここでmはシーケスの長さとpossiblePrefixの長さのより短い方です。