Last active
November 20, 2016 16:14
-
-
Save TBXark/2fd260f2c8586c402db00dd89479a416 to your computer and use it in GitHub Desktop.
Find all locations of substring in NSString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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