String.UnicodeScalarView.String.UnicodeScalarIndex

extension String.UnicodeScalarView.String.UnicodeScalarIndex
Import import Swift

Initializers

init(_:within:)

Creates an index in the given Unicode scalars view that corresponds exactly to the specified string position.

The following example converts the position of the teacup emoji ("🍵") into its corresponding position in the string's unicodeScalars view.

let cafe = "Café 🍵"
let characterIndex = cafe.characters.index(of: "🍵")!
let scalarIndex = String.UnicodeScalarView.Index(characterIndex, within: cafe.unicodeScalars)

print(cafe.unicodeScalars.suffix(from: scalarIndex))
// Prints "🍵"

Parameters: characterIndex: A position in a CharacterView instance. characterIndex must be an element of String(utf8).characters.indices. utf8: The UTF8View in which to find the new position.

Declaration

init(_ characterIndex: String.Index, within unicodeScalars: String.UnicodeScalarView)
init?(_: String.UTF8Index, within: String.UnicodeScalarView)

Creates an index in the given Unicode scalars view that corresponds exactly to the specified UTF8View position.

If the position passed as utf8Index doesn't have an exact corresponding position in unicodeScalars, the result of the initializer is nil. For example, an attempt to convert the position of a UTF-8 continuation byte returns nil.

Parameters: utf8Index: A position in the utf8 view of the characters parameter. unicodeScalars: The UnicodeScalarView instance referenced by both utf8Index and the resulting index.

Declaration

init?(_ utf8Index: String.UTF8Index, within unicodeScalars: String.UnicodeScalarView)
init?(_: String.UTF16Index, within: String.UnicodeScalarView)

Creates an index in the given Unicode scalars view that corresponds exactly to the specified UTF16View position.

The following example finds the position of a space in a string's utf16 view and then converts that position to an index in the string's unicodeScalars view:

let cafe = "Café 🍵"

let utf16Index = cafe.utf16.index(of: 32)!
let scalarIndex = String.UnicodeScalarView.Index(utf16Index, within: cafe.unicodeScalars)!

print(String(cafe.unicodeScalars.prefix(upTo: scalarIndex)))
// Prints "Café"

If the position passed in utf16Index doesn't have an exact corresponding position in unicodeScalars, the result of the initializer is nil. For example, an attempt to convert the position of the trailing surrogate of a UTF-16 surrogate pair fails.

Parameters: utf16Index: A position in the utf16 view of the characters parameter. unicodeScalars: The UnicodeScalarView instance referenced by both utf16Index and the resulting index.

Declaration

init?(_ utf16Index: String.UTF16Index, within unicodeScalars: String.UnicodeScalarView)

Instance Methods

func samePosition(in: String)

Returns the position in the given string that corresponds exactly to this index.

This index must be a valid index of characters.unicodeScalars.

This example first finds the position of a space (UTF-8 code point 32) in a string's utf8 view and then uses this method find the same position in the string.

let cafe = "Café 🍵"
let i = cafe.unicodeScalars.index(of: "🍵")
let j = i.samePosition(in: cafe)!
print(cafe.suffix(from: j))
// Prints "🍵"

characters: The string to use for the index conversion. Returns: The position in characters that corresponds exactly to this index. If this index does not have an exact corresponding position in characters, this method returns nil. For example, an attempt to convert the position of a UTF-8 continuation byte returns nil.

Declaration

func samePosition(in characters: String) -> String.Index?
func samePosition(in: String.UTF8View)

Returns the position in the given UTF-8 view that corresponds exactly to this index.

The index must be a valid index of String(utf8).unicodeScalars.

This example first finds the position of the character "é" and then uses this method find the same position in the string's utf8 view.

let cafe = "Café"
if let i = cafe.unicodeScalars.index(of: "é") {
    let j = i.samePosition(in: cafe.utf8)
    print(Array(cafe.utf8.suffix(from: j)))
}
// Prints "[195, 169]"

utf8: The view to use for the index conversion. Returns: The position in utf8 that corresponds exactly to this index.

Declaration

func samePosition(in utf8: String.UTF8View) -> String.UTF8View.Index
func samePosition(in: String.UTF16View)

Returns the position in the given UTF-16 view that corresponds exactly to this index.

The index must be a valid index of String(utf16).unicodeScalars.

This example first finds the position of the character "é" and then uses this method find the same position in the string's utf16 view.

let cafe = "Café"
if let i = cafe.characters.index(of: "é") {
    let j = i.samePosition(in: cafe.utf16)
    print(cafe.utf16[j])
}
// Prints "233"

utf16: The view to use for the index conversion. Returns: The position in utf16 that corresponds exactly to this index.

Declaration

func samePosition(in utf16: String.UTF16View) -> String.UTF16View.Index