Instance Methodswift 6.0.1Swift
samePosition(in:)
Returns the position in the given view of Unicode scalars that corresponds exactly to this index.
func samePosition(in unicodeScalars: String.UnicodeScalarView) -> String.UnicodeScalarIndex?
Parameters
- unicodeScalars
The view to use for the index conversion. This index must be a valid index of at least one view of the string shared by
unicodeScalars
.
Returns
The position in unicodeScalars
that corresponds exactly to this index. If this index does not have an exact corresponding position in unicodeScalars
, this method returns nil
. For example, an attempt to convert the position of a UTF-16 trailing surrogate returns nil
.
This index must be a valid index of String(unicodeScalars).utf16
.
This example first finds the position of a space (UTF-16 code point 32
) in a string’s utf16
view and then uses this method to find the same position in the string’s unicodeScalars
view.
let cafe = "Café 🍵"
let i = cafe.utf16.firstIndex(of: 32)!
let j = i.samePosition(in: cafe.unicodeScalars)!
print(String(cafe.unicodeScalars[..<j]))
// Prints "Café"
Other members in extension
Type members
init?(String.Index, within: String.UnicodeScalarView
) Creates an index in the given Unicode scalars view that corresponds exactly to the specified
UTF16View
position.init?(String.Index, within: String.UTF8View
) Creates an index in the given UTF-8 view that corresponds exactly to the specified
UTF16View
position.init?(String.Index, within: String.UTF16View
) Creates an index in the given UTF-16 view that corresponds exactly to the specified string position.
init?(String.Index, within: String
) Creates an index in the given string that corresponds exactly to the specified position.
init?<S>(String.Index, within: S
) Creates an index in the given string that corresponds exactly to the specified position.
init<S>(utf16Offset: Int, in: S
) Creates a new index at the specified UTF-16 code unit offset
static func < (lhs: String.Index, rhs: String.Index
) -> Bool static func == (lhs: String.Index, rhs: String.Index
) -> Bool
Show obsolete interfaces (1)
Hide obsolete interfaces
init(encodedOffset: Int
) Creates a new index at the specified code unit offset.
Instance members
func hash(into: inout Hasher
) Hashes the essential components of this value by feeding them into the given hasher.
func samePosition(in: String.UTF8View
) -> String.UTF8View.Index? Returns the position in the given UTF-8 view that corresponds exactly to this index.
func samePosition(in: String.UTF16View
) -> String.UTF16View.Index? Returns the position in the given UTF-16 view that corresponds exactly to this index.
func samePosition(in: String
) -> String.Index? Returns the position in the given string that corresponds exactly to this index.
func utf16Offset<S>(in: S
) -> Int The UTF-16 code unit offset corresponding to this index.
Show obsolete interfaces (1)
Hide obsolete interfaces
var encodedOffset: Int
The offset into a string’s code units for this index.