當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Swift String.Index init(_:within:)用法及代碼示例

用法一

初始化器

init(_:within:)

在給定的 UTF-16 視圖中創建與指定字符串位置完全對應的索引。

聲明

init?(
    _ idx: String.Index,
    within target: String.UTF16View
)

參數

sourcePosition

target 共享的字符串的至少一個視圖中的位置。

target

找到新位置的UTF16View

詳述

如果作為 sourcePosition 傳遞的索引表示 Unicode 標量值的開始或 UTF-16 尾隨代理項的位置,則初始化程序成功。如果 sourcePositiontarget 中沒有精確對應的位置,則結果為 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é"

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法二

初始化器

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"

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法三

初始化器

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"

可用版本

iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+

用法四

初始化器

init(_:within:)

在給定的 UTF-8 視圖中創建與指定的 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"

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法五

初始化器

init(_:within:)

在給定的 Unicode 標量視圖中創建與指定的 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

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

相關用法


注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 String.Index init(_:within:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。