用法一
init(_:within:)
聲明
init?(
_ idx: String.Index,
within target: String.UTF16View
)
參數
sourcePosition
target
共享的字符串的至少一個視圖中的位置。target
找到新位置的
UTF16View
。
詳述
如果作為 sourcePosition
傳遞的索引表示 Unicode 標量值的開始或 UTF-16 尾隨代理項的位置,則初始化程序成功。如果 sourcePosition
在 target
中沒有精確對應的位置,則結果為 nil
。例如,嘗試轉換 UTF-8 連續字節的位置會導致 nil
。
以下示例查找字符串中空格的位置,然後將該位置轉換為字符串的 utf16
視圖中的索引。
let cafe = "Café 🍵"
let stringIndex = cafe.firstIndex(of: "é")!
let utf16Index = String.Index(stringIndex, within: cafe.utf16)!
print(String(cafe.utf16[...utf16Index])!)
// Prints "Café"
可用版本
用法二
init(_:within:)
聲明
init?(
_ sourcePosition: String.Index,
within target: String
)
參數
sourcePosition
target
參數視圖中的位置。sourcePosition
必須是target
的至少一個視圖的有效索引。target
結果索引引用的字符串。
詳述
如果作為sourcePosition
傳遞的索引表示擴展字素簇的開始——字符串的元素類型——則初始化程序成功。
以下示例將 Unicode 標量 "e"
的位置轉換為其在字符串中的對應位置。該位置的字符是組合的"é"
字符。
let cafe = "Cafe\u{0301}"
print(cafe)
// Prints "Café"
let scalarsIndex = cafe.unicodeScalars.firstIndex(of: "e")!
let stringIndex = String.Index(scalarsIndex, within: cafe)!
print(cafe[...stringIndex])
// Prints "Café"
如果作為 sourcePosition
傳遞的索引在 target
中沒有精確對應的位置,則初始化程序的結果是 nil
。例如,嘗試轉換組合銳音符 ("\u{0301}"
) 的位置失敗。組合 Unicode 標量在字符串中沒有自己的位置。
let nextScalarsIndex = cafe.unicodeScalars.index(after: scalarsIndex)
let nextStringIndex = String.Index(nextScalarsIndex, within: cafe)
print(nextStringIndex)
// Prints "nil"
可用版本
用法三
init(_:within:)
聲明
init?<S>(
_ sourcePosition: String.Index,
within target: S
) where S : StringProtocol
參數
sourcePosition
target
參數視圖中的位置。sourcePosition
必須是target
的至少一個視圖的有效索引。target
結果索引引用的字符串。
詳述
如果作為sourcePosition
傳遞的索引表示擴展字素簇的開始——字符串的元素類型——則初始化程序成功。
以下示例將 Unicode 標量 "e"
的位置轉換為其在字符串中的對應位置。該位置的字符是組合的"é"
字符。
let cafe = "Cafe\u{0301}"
print(cafe)
// Prints "Café"
let scalarsIndex = cafe.unicodeScalars.firstIndex(of: "e")!
let stringIndex = String.Index(scalarsIndex, within: cafe)!
print(cafe[...stringIndex])
// Prints "Café"
如果作為 sourcePosition
傳遞的索引在 target
中沒有精確對應的位置,則初始化程序的結果是 nil
。例如,嘗試轉換組合銳音符 ("\u{0301}"
) 的位置失敗。組合 Unicode 標量在字符串中沒有自己的位置。
let nextScalarsIndex = cafe.unicodeScalars.index(after: scalarsIndex)
let nextStringIndex = String.Index(nextScalarsIndex, within: cafe)
print(nextStringIndex)
// Prints "nil"
可用版本
用法四
init(_:within:)
UTF16View
位置完全對應的索引。聲明
init?(
_ idx: String.Index,
within target: String.UTF8View
)
參數
sourcePosition
String
或其視圖之一中的位置。target
找到新位置的
UTF8View
。
詳述
以下示例在字符串的 utf16
視圖中查找空格的位置,然後將該位置轉換為字符串的 utf8
視圖中的索引。
let cafe = "Café 🍵"
let utf16Index = cafe.utf16.firstIndex(of: 32)!
let utf8Index = String.UTF8View.Index(utf16Index, within: cafe.utf8)!
print(Array(cafe.utf8[..<utf8Index]))
// Prints "[67, 97, 102, 195, 169]"
如果在 utf16Index
中傳遞的位置在 utf8
中沒有精確對應的位置,則初始化器的結果是 nil
。例如,由於 UTF-8 和 UTF-16 以不同的方式表示高 Unicode 代碼點,因此嘗試轉換 UTF-16 代理項對的尾隨代理項的位置會失敗。
下一個示例嘗試轉換代表茶杯表情符號 ("🍵"
) 的兩個 UTF-16 代碼點的索引。前導代理的索引已成功轉換為 utf8
中的位置,但尾隨代理的索引不是。
let emojiHigh = cafe.utf16.index(after: utf16Index)
print(String.UTF8View.Index(emojiHigh, within: cafe.utf8))
// Prints "Optional(String.Index(...))"
let emojiLow = cafe.utf16.index(after: emojiHigh)
print(String.UTF8View.Index(emojiLow, within: cafe.utf8))
// Prints "nil"
可用版本
用法五
init(_:within:)
UTF16View
位置完全對應的索引。聲明
init?(
_ sourcePosition: String.Index,
within unicodeScalars: String.UnicodeScalarView
)
參數
sourcePosition
字符串的
utf16
視圖中的位置。utf16Index
必須是String(unicodeScalars).utf16.indices
的一個元素。unicodeScalars
找到新位置的
UnicodeScalarView
。
詳述
以下示例在字符串的 utf16
視圖中查找空格的位置,然後將該位置轉換為字符串的 unicodeScalars
視圖中的索引:
let cafe = "Café 🍵"
let utf16Index = cafe.utf16.firstIndex(of: 32)!
let scalarIndex = String.Index(utf16Index, within: cafe.unicodeScalars)!
print(String(cafe.unicodeScalars[..<scalarIndex]))
// Prints "Café"
如果作為 sourcePosition
傳遞的索引在 unicodeScalars
中沒有精確對應的位置,則初始化程序的結果是 nil
。例如,嘗試轉換 UTF-16 代理對的尾隨代理的位置會導致 nil
。
可用版本
相關用法
- Swift String.Index ..<(_:_:)用法及代碼示例
- Swift String.Index ...(_:_:)用法及代碼示例
- Swift String.Index ..<(_:)用法及代碼示例
- Swift String.Index samePosition(in:)用法及代碼示例
- Swift String.Index ...(_:)用法及代碼示例
- Swift String.Iterator next()用法及代碼示例
- Swift String.UTF8View first用法及代碼示例
- Swift String.UTF16View firstIndex(where:)用法及代碼示例
- Swift String.UTF16View last(where:)用法及代碼示例
- Swift String.UnicodeScalarView flatMap(_:)用法及代碼示例
- Swift String.UTF8View split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift String.UTF8View elementsEqual(_:)用法及代碼示例
- Swift String.UnicodeScalarView min(by:)用法及代碼示例
- Swift String.UnicodeScalarView lastIndex(of:)用法及代碼示例
- Swift String.UTF8View last(where:)用法及代碼示例
- Swift String.UTF8View split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift String.UnicodeScalarView filter(_:)用法及代碼示例
- Swift String.UTF8View debugDescription用法及代碼示例
- Swift String.UTF16View flatMap(_:)用法及代碼示例
- Swift String.UTF16View min(by:)用法及代碼示例
- Swift String.UTF16View shuffled(using:)用法及代碼示例
- Swift String.UTF8View isEmpty用法及代碼示例
- Swift String.UTF16View min()用法及代碼示例
- Swift String.UTF8View starts(with:)用法及代碼示例
- Swift String.UTF16View isEmpty用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 String.Index init(_:within:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。