I want to search backwards in a string. But all examples I found are not running on current Swift 3.0. ".BackwardsSearch" is not an option available on my XCode 8. Any idea, how this can be done? Example:
let rPos = rIndex(sourceString, searchString)
or
let rPos = sourceString.rindex(of: searchString)
XCode autocomplete will suggest this:
let rpos = sourceString.range(of:searchString, options:String.CompareOptions.backwards, range:nil, locale:nil)
However, range and locale have defaults of nil, so you can omit them:
let rpos = sourceString.range(of:searchString, options:String.CompareOptions.backwards)
And options
is of the type String.CompareOptions
so you only need the last part:
let rpos = sourceString.range(of:searchString, options:.backwards)
(Thanks @MartinR for pointing out the shortened versions)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With