Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stride String
extension String {
func stride(by distance: String.IndexDistance, substring: (String.SubSequence) -> Void) {
guard distance > 0, distance < count else {
substring(self[...])
return
}
var i = index(startIndex, offsetBy: distance, limitedBy: endIndex) ?? endIndex
var previ = startIndex
while i < endIndex {
substring(self[previ..<i])
previ = i
i = index(i, offsetBy: distance, limitedBy: endIndex) ?? endIndex
}
substring(self[previ..<i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment