I know how to find an index of a character like the following:
func indexOfCharacter(myString:String, char: Character) -> Int {
if let idx = find(mySt
Try like this:
func indexOfCharacter(myString:String, char: Character,fromIndex: Int) -> Int {
let sub = myString.substringFromIndex(advance(myString.startIndex, fromIndex))
if let idx = find(sub, char) {
return distance(sub.startIndex, idx) + fromIndex
}else{
return -1
}
}
let myString = "abcdabcd"
indexOfCharacter(myString, "d", 4) // 7
update: Xcode 7.3 • Swift 2.2
func indexOfCharacter(myString:String, char: Character, fromIndex: Int) -> Int {
let sub = myString.substringFromIndex(myString.startIndex.advancedBy(fromIndex))
if let idx = sub.characters.indexOf(char){
return sub.startIndex.distanceTo(idx) + fromIndex
}else{
return -1
}
}