Skip to content

Instantly share code, notes, and snippets.

@dragouf
Last active April 21, 2016 07:41
Show Gist options
  • Save dragouf/e8f30a3fdecbee0af046 to your computer and use it in GitHub Desktop.
Save dragouf/e8f30a3fdecbee0af046 to your computer and use it in GitHub Desktop.
swift String 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