I have always had a function to get the substring range of a string. Here is my updated function for Swift 3:
func getSubStringRange(fullString: String, fromIndex: Int, subStringSize: Int) -> Range<String.Index> {
let startIndex = fullString.characters.index(fullString.startIndex, offsetBy: fromIndex)
let endIndex = fullString.characters.index(startIndex, offsetBy: subStringSize)
let subStringRange = startIndex..<endIndex
return subStringRange
}
The function is pretty self explanatory - You pass in a string(fullString), the index of that string where the substring starts(fromIndex) and how big the subString is(subStringSize).
Example:
let greeting = "Hi, my name is Nathaniel"
let getName = greeting[getSubStringRange(fullString: greeting, fromIndex: 15, subStringSize: 9)]
print("Name: \(getName)")
-> Prints: "Name: Nathaniel"