Skip to content

Instantly share code, notes, and snippets.

@TBXark
Last active November 20, 2016 16:14
Show Gist options
  • Save TBXark/2fd260f2c8586c402db00dd89479a416 to your computer and use it in GitHub Desktop.
Save TBXark/2fd260f2c8586c402db00dd89479a416 to your computer and use it in GitHub Desktop.
Find all locations of substring in NSString
extension NSString {
func rangesOfSubString(_ string: String) -> [NSRange] {
var searchRange = NSMakeRange(0, length)
var searchResult = [NSRange]()
while searchRange.location < length {
searchRange.length = length - searchRange.location
let foundRange = range(of: string, options: [], range: searchRange)
if foundRange.location != NSNotFound {
searchResult.append(foundRange)
searchRange.location = foundRange.location+foundRange.length
} else {
break
}
}
return searchResult
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment