You can use the lastIndex(of:)
method to find the last index of a substring in a string in Swift. Here's an example:
main.swift221 chars9 lines
This code will output:
main.swift45 chars2 lines
Note that lastIndex(of:)
returns an optional String.Index
because the substring might not be present in the string. You can use optional binding or a guard statement to check if the index is not nil before using it.
gistlibby LogSnag