Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active February 2, 2017 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjhomer/ffbea77cc8bc0dbc27878031abfb1148 to your computer and use it in GitHub Desktop.
Save bjhomer/ffbea77cc8bc0dbc27878031abfb1148 to your computer and use it in GitHub Desktop.
Incrementally searching through a string in Swift
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