swift string index of substring code example
Example 1: index string swift
let string = "Hello, World!"
let index = string.index(string.startIndex, offsetBy: 3)
print(string[index]) // Prints "l"
// This can be done in one line too:
print(string[string.index(string.startIndex, offsetBy: 3)])
Example 2: how can i find range of a string in another string swift
let str = "abcde"
if let range = str.range(of: "cd") {
let substring = str[..<range.lowerBound] // or str[str.startIndex..<range.lowerBound]
print(substring) // Prints ab
}
else {
print("String not present")
}