Skip to content

Instantly share code, notes, and snippets.

@ericcgu
Created January 23, 2015 05:26
Show Gist options
  • Save ericcgu/c1507162b97c4534f0c7 to your computer and use it in GitHub Desktop.
Save ericcgu/c1507162b97c4534f0c7 to your computer and use it in GitHub Desktop.
String Range Extension
extension String {
subscript (i: Int) -> Character {
return self[advance(self.startIndex, i)]
}
subscript (i: Int) -> String {
return String(self[i] as Character)
}
subscript (r: Range<Int>) -> String {
return substringWithRange(Range(start: advance(startIndex, r.startIndex), end: advance(startIndex, r.endIndex)))
}
}
"abcde"[0] === "a"
"abcde"[0...2] === "abc"
"abcde"[2..<4] === "cd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment