I want to find the index of the last occurrence of a character in a String. For example if my string is "google.com/program/test"
and I want to find the letter /
, I want my function to return 18
because that is the last occurrence of /
in that string. I have tried to follow Finding index of character in Swift String and have also tried to implement a means to loop through the string and simply find the last index of the String that has my desired character but the advance()
function seems to complain about getting an Integer.
var strCount = 0
var lastSlashIndex = 0
for i in urlAsString {
if i == "/"{
if lastSlashIndex < strCount{
lastSlashIndex = strCount
}
}
strCount++
}
var endOfString = strCount - lastSlash
//Cannot seem to use the advance function to get the remaining substring
var fileName = urlAsString.substringFromIndex(advance(lastSlashIndex, endOfString))
Can't seem to figure this out, any help would be appreciated.
EDIT:
This problem is not ment to be specific to just the '/' character. For example, if the string is "abbccd"
and I'm looking for the letter 'c'
, then I want to return the index 4
because that is the last index in which 'c'
occurs in.