Last active
September 28, 2016 13:27
-
-
Save mono0926/e987807d54b3c43c8ccecc5b1250de7a to your computer and use it in GitHub Desktop.
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
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