I have a slice function which I got here. I was wondering how I can modify it so that if the to
string is not found, but it found from
it will return the end index of the entire string (.count-1
). Right now it's obviously crashing if I call .slice
and there is no to
string found.
extension String {
func slice(from: String, to: String) -> String? {
return (range(of: from)?.upperBound).flatMap { substringFrom in
(range(of: to, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
String(self[substringFrom..<substringTo])
}
}
}
}