實例方法
has
hasPrefix(_:)
返回一個布爾值,指示字符串是否以指定的前綴開頭。
聲明
func hasPrefix<Prefix>(_ prefix: Prefix) -> Bool where Prefix : StringProtocol
返回值
true
如果字符串以 prefix
開頭;否則,false
。
參數
prefix
針對此字符串進行測試的可能前綴。
詳述
比較是區分大小寫和 Unicode 安全的。區分大小寫的比較隻會匹配對應字符大小寫相同的字符串。
let cafe = "Café du Monde"
// Case sensitive
print(cafe.hasPrefix("café"))
// Prints "false"
Unicode-safe 比較匹配 Unicode 擴展字素簇,而不是用於組成它們的代碼點。下麵的示例使用兩個具有不同形式的"é"
字符的字符串——第一個使用組合形式,第二個使用分解形式。
// Unicode safe
let composedCafe = "Café"
let decomposedCafe = "Cafe\u{0301}"
print(cafe.hasPrefix(composedCafe))
// Prints "true"
print(cafe.hasPrefix(decomposedCafe))
// Prints "true"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Substring hasSuffix(_:)用法及代碼示例
- Swift Substring last用法及代碼示例
- Swift Substring remove(at:)用法及代碼示例
- Swift Substring reversed()用法及代碼示例
- Swift Substring split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift Substring first用法及代碼示例
- Swift Substring ...(_:_:)用法及代碼示例
- Swift Substring randomElement(using:)用法及代碼示例
- Swift Substring contains(where:)用法及代碼示例
- Swift Substring init(repeating:count:)用法及代碼示例
- Swift Substring prefix(upTo:)用法及代碼示例
- Swift Substring firstIndex(where:)用法及代碼示例
- Swift Substring replaceSubrange(_:with:)用法及代碼示例
- Swift Substring ..<(_:)用法及代碼示例
- Swift Substring ..<(_:_:)用法及代碼示例
- Swift Substring removeAll(where:)用法及代碼示例
- Swift Substring allSatisfy(_:)用法及代碼示例
- Swift Substring isEmpty用法及代碼示例
- Swift Substring lastIndex(of:)用法及代碼示例
- Swift Substring ...(_:)用法及代碼示例
- Swift Substring last(where:)用法及代碼示例
- Swift Substring +=(_:_:)用法及代碼示例
- Swift Substring max(by:)用法及代碼示例
- Swift Substring shuffled(using:)用法及代碼示例
- Swift Substring description用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Substring hasPrefix(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。