Instance Subscriptswift 6.0.1Swift
subscript(_:)
Accesses the Unicode scalar value at the given position.
subscript(position: String.UnicodeScalarView.Index) -> Unicode.Scalar { get }
Parameters
- position
A valid index of the character view.
position
must be less than the view’s end index.
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"
Other members in extension
Types
Typealiases
Type members
init(
) Creates an empty view instance.
Instance members
var customMirror: Mirror
Returns a mirror that reflects the Unicode scalars view of a string.
var debugDescription: String
var description: String
var endIndex: String.UnicodeScalarView.Index
The “past the end” position—that is, the position one greater than the last valid subscript argument.
var startIndex: String.UnicodeScalarView.Index
The position of the first Unicode scalar value if the string is nonempty.
subscript(Range<String.UnicodeScalarView.Index>
) -> String.UnicodeScalarView.SubSequence func append(Unicode.Scalar
) Appends the given Unicode scalar to the view.
func append<S>(contentsOf: S
) Appends the Unicode scalar values in the given sequence to the view.
func distance(from: String.UnicodeScalarView.Index, to: String.UnicodeScalarView.Index
) -> Int func index(String.UnicodeScalarView.Index, offsetBy: Int
) -> String.UnicodeScalarView.Index func index(String.UnicodeScalarView.Index, offsetBy: Int, limitedBy: String.UnicodeScalarView.Index
) -> String.UnicodeScalarView.Index? func index(after: String.UnicodeScalarView.Index
) -> String.UnicodeScalarView.Index Returns the next consecutive location after
i
.func index(before: String.UnicodeScalarView.Index
) -> String.UnicodeScalarView.Index Returns the previous consecutive location before
i
.func makeIterator(
) -> String.UnicodeScalarView.Iterator func replaceSubrange<C>(Range<String.UnicodeScalarView.Index>, with: C
) Replaces the elements within the specified bounds with the given Unicode scalar values.
func reserveCapacity(Int
) Reserves enough space in the view’s underlying storage to store the specified number of ASCII characters.