Last active
November 28, 2017 06:35
-
-
Save JacopoMangiavacchi/b918515412ba441c0277d3d06fbb82ad 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
import Foundation | |
extension String { | |
subscript(value: PartialRangeUpTo<Int>) -> Substring { | |
get { | |
return self[..<index(startIndex, offsetBy: value.upperBound)] | |
} | |
} | |
subscript(value: PartialRangeThrough<Int>) -> Substring { | |
get { | |
return self[...index(startIndex, offsetBy: value.upperBound)] | |
} | |
} | |
subscript(value: PartialRangeFrom<Int>) -> Substring { | |
get { | |
return self[index(startIndex, offsetBy: value.lowerBound)...] | |
} | |
} | |
subscript(value: CountableRange<Int>) -> Substring { | |
get { | |
return self[index(startIndex, offsetBy: value.lowerBound)..<index(startIndex, offsetBy: value.upperBound)] | |
} | |
} | |
subscript(value: CountableClosedRange<Int>) -> Substring { | |
get { | |
return self[index(startIndex, offsetBy: value.lowerBound)...index(startIndex, offsetBy: value.upperBound)] | |
} | |
} | |
subscript(value: Int) -> Substring { | |
get { | |
return self[index(startIndex, offsetBy: value)..<index(startIndex, offsetBy: value+1)] | |
} | |
} | |
} | |
// let text = "Hello world" | |
// text[...4] // "Hello" | |
// text[5] // " " | |
// text[6...] // "world" | |
// text[3...] // "lo world" | |
// text[3..<7] // "lo w" | |
// text[3...7] // "lo wo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment