I want to find the first occurrence of string starting at given index.
Based on this answer I have created following function:
func index(of string: String, from startIndex: String.Index? = nil, options: String.CompareOptions = .literal) -> String.Index? {
if let startIndex = startIndex {
return range(of: string, options: options, range: startIndex ..< string.endIndex, locale: nil)?.lowerBound
} else {
return range(of: string, options: options, range: nil, locale: nil)?.lowerBound
}
}
unfortunately, the part with the index doesn't work.
For example following code returns nil
instead of 3
:
let str = "test"
str.index(of: "t", from: str.index(str.startIndex, offsetBy: 1))