Last active
February 2, 2017 16:45
-
-
Save bjhomer/ffbea77cc8bc0dbc27878031abfb1148 to your computer and use it in GitHub Desktop.
Incrementally searching through a string in Swift
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
import Foundation | |
let str = "abXcdXefXg" | |
let start = str.startIndex | |
var searchStart = start | |
while let xRange = str.range(of: "X", range: searchStart..<str.endIndex) { | |
searchStart = xRange.upperBound | |
let substringToThisPoint = str[start..<xRange.lowerBound] | |
print("Substring up to this X:", substringToThisPoint) | |
} | |
// Output: | |
// Substring up to this X: ab | |
// Substring up to this X: abXcd | |
// Substring up to this X: abXcdXef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment