find the last index of a character in a string in swift

To find the last index of a character in a string, you can use the built-in lastIndex(of:) method. This method returns an optional index that specifies the position of the last occurrence of the given character in the string. If the character is not found in the string, lastIndex(of:) returns nil.

Here's an example code snippet that demonstrates how to use lastIndex(of:) to find the last index of 'l' in the string "Hello, world!":

main.swift
let str = "Hello, world!"
if let lastIndex = str.lastIndex(of: "l") {
    print("The last index of 'l' is \(lastIndex).")
} else {
    print("'l' was not found in the string.")
}
179 chars
7 lines

This will output: "The last index of 'l' is 10."

gistlibby LogSnag