Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active September 28, 2016 13:27
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 mono0926/e987807d54b3c43c8ccecc5b1250de7a to your computer and use it in GitHub Desktop.
Save mono0926/e987807d54b3c43c8ccecc5b1250de7a to your computer and use it in GitHub Desktop.
extension String.CharacterView {
subscript(sequentialAccess i: Int) -> Character {
let index = self.index(startIndex, offsetBy: i)
return self[index]
}
subscript(sequentialAccess range: Range<Int>) -> String.CharacterView {
let start = self.index(startIndex, offsetBy: range.lowerBound)
let end = self.index(start, offsetBy: range.count)
return self[start..<end]
}
subscript(sequentialAccess range: ClosedRange<Int>) -> String.CharacterView {
let start = self.index(startIndex, offsetBy: range.lowerBound)
let end = self.index(start, offsetBy: range.count - 1)
return self[start...end]
}
}
let s = "12345"
s.characters[sequentialAccess: 1]
// → 2
String(s.characters[sequentialAccess: 1..<3])
// → 23
String(s.characters[sequentialAccess: 1...3])
// → 234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment