Accesses the Unicode scalar value at the given position.
SDK
- Xcode 7.1+
Framework
- Swift Standard Library
Declaration
subscript(position: String.Unicode Scalar View.Index) -> Unicode.Scalar { get }
Parameters
positionA valid index of the character view.
positionmust be less than the view’s end index.
Discussion
The following example searches a string’s Unicode scalars view for a capital letter and then prints the character and Unicode scalar value at the found index:
let greeting = "Hello, friend!"
if let i = greeting.unicodeScalars.firstIndex(where: { "A"..."Z" ~= $0 }) {
print("First capital letter: \(greeting.unicodeScalars[i])")
print("Unicode scalar value: \(greeting.unicodeScalars[i].value)")
}
// Prints "First capital letter: H"
// Prints "Unicode scalar value: 72"