Last active
December 19, 2017 09:55
-
-
Save zigzagg16/cd7668fecd9871c2382be71d4ff6cf7d to your computer and use it in GitHub Desktop.
Swift String : rangesOf substrings
This file contains hidden or 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 String { | |
func ranges(of subString: String) -> [NSRange] { | |
var result: [NSRange] = [] | |
var start = startIndex | |
while let range = range(of: subString, options: .literal, range: start..<endIndex) { | |
let startPos = self.distance(from: self.startIndex, to: range.lowerBound) | |
let endPos = self.distance(from: self.startIndex, to: range.upperBound) | |
result.append(NSMakeRange(startPos, (endPos - startPos))) | |
start = range.upperBound | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment