str[i], str[i..<j], str[i...j]
๐ replit
extension String {
/// โญ example: `str[5]`
public subscript(_ i: Int) -> String? {
// โญ guard: non-empty string, non-negative index
guard !isEmpty, i >= 0 else { return nil }
// โญ safe bound
let bound = index(before: endIndex) // โญ Note: `endIndex` is NOT safe
// โญ guard: startIndex <= index < endIndex
guard let index = index(startIndex, offsetBy: i, limitedBy: bound) else {
return nil
}
// โญ Character -> String
return String(self[index])
}
/// example: `str[2..<5]`
public subscript(bounds: Range<Int>) -> String? {
// โญ guard: empty string
guard !isEmpty else { return nil } // empty string -> nil
// โญ guard: 0 <= m < n
let (m, n) = (bounds.lowerBound, bounds.upperBound)
guard 0 <= m, m < n else { return nil } // negative index -> nil
// โญ guard: i < endIndex, j <= endIndex
let bound = index(before: endIndex)
guard let i = index(startIndex, offsetBy: m, limitedBy: bound) else {
return nil
}
guard let j = index(startIndex, offsetBy: n, limitedBy: endIndex) else {
return nil
}
// i, j are in safe range
return String(self[i..<j])
}
/// example: `str[2...5]`
public subscript(bounds: ClosedRange<Int>) -> String? {
// โญ guard: empty string
guard !isEmpty else { return nil } // empty string -> nil
// โญ guard: 0 <= m < n
let (m, n) = (bounds.lowerBound, bounds.upperBound)
guard 0 <= m, m <= n else { return nil } // negative index -> nil
// โญ guard: i < endIndex, j <= endIndex
let bound = index(before: endIndex)
guard let i = index(startIndex, offsetBy: m, limitedBy: bound) else {
return nil
}
guard let j = index(startIndex, offsetBy: n, limitedBy: bound) else {
return nil
}
// i, j are in safe range
return String(self[i...j])
}
}
๐ replit
// 01234
let s = "Swift"
s[1] // String?: "w"
s[5] // nil
// half-open range
s[0..<3], // Optional("Swi")
s[2..<5], // Optional("ift")
s[1..<1], // nil
s[1..<9], // nil
s[-3..<2], // nil
// closed range
s[0...3], // Optional("Swif")
s[2...5], // nil
s[1...1], // Optional("w")
s[1...9], // nil
s[-3...2], // nil
Swift โฉ
Collections โฉ
Range<Bound> - (generic struct)
subscript(_:) -
arr[i..<j]
,str[i..<j]
้ฝๆฏ override ้ๅๆนๆณ
Last updated