Swift Substring

Swift Substring

Swift SubString 操作
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 {
            let start = index(self.startIndex, offsetBy: value.lowerBound)
            let end = index(self.startIndex, offsetBy: value.upperBound)
            return self[start..<end]
        }
    }
    
    subscript(value: ClosedRange<Int>) -> Substring {
        get {
            let start = index(self.startIndex, offsetBy: value.lowerBound)
            let end = index(self.startIndex, offsetBy: value.upperBound)
            return self[start...end]
        }
    }
}

let str = "hello"
// PartialRangeFrom
print(str[1...]) // ello
// PartialRangeThrough
print(str[...3]) //  hell
// PartialRangeUpTo
print(str[..<3]) // hel
// ClosedRange
print(str[1...3]) // ell
// CountableRange
print(str[1..<3]) // el
Edit Edit this page

本作品采用 知识署名-非商业性使用-禁止演绎 (BY-NC-ND) 4.0 国际许可协议 进行许可。