Issue #910
Use one-sided range operator
let string = "Hello world"
string[string.startIndex...] // Hello world
string[..<string.endIndex] // Hello world
Substring
let string = "Hello world"
let range = string.startIndex ..< string.index(string.startIndex, offsetBy: 5)
string[range] // Hello
Convert to and from NSRange
let string = "Hello world"
let range = string.startIndex...
let nsRange = NSRange(range, in: string)
let regex = NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: string, range: nsRange)
for match in matches {
let range = Range(match.range, in: string)
let subString = String(string[range])
}
Check Range<Int>
is valid
extension String {
func isValid(range: Range<Int>) -> Bool {
index(startIndex, offsetBy: range.lowerBound, limitedBy: endIndex) != nil
&& index(startIndex, offsetBy: range.upperBound, limitedBy: endIndex) != nil
}
}