美文网首页iOS 常见问题
Swift Range和字符串截取

Swift Range和字符串截取

作者: 梦里桃花舞倾城 | 来源:发表于2020-09-22 17:44 被阅读0次

Range<T>

  • ClosedRange: a...b
  • CountableClosedRange: a...b
  • Range: a..<b
  • CountRange: a..<b
  • PartialRangeThrough: ...b
  • PartialRangeFrom: a...
  • PartialRangeUpTo: ..<b

字符串截取

let str = "123456789"
let start = str.startIndex//表示str的开始位置
let end = str.endIndex//表示str的结束位置
let startOffset = str.index(start, offsetBy: 2)//表示str的开始位置 + 2
let endOffset = str.index(end, offsetBy: -2)//表示str的结束位置 - 2
        
print(str[start])//输出 1 第1个字符
print(str[startOffset])//输出 3 第3个字符
print(str[endOffset])//输出 8 第8个字符(10-2)
print(str[end])//报错!因为实endIndex指向第10个字符,而第10个字符是不存在的

String Extension

    /// 根据下标获取某个下标字符
    subscript(of index: Int) -> String {
        if index < 0 || index >= self.count{
            return ""
        }
        for (i,item) in self.enumerated(){
            if index == i {
                return "\(item)"
            }
        }
        return ""
    }
    /// 根据range获取字符串 a[1...3]
    subscript(r: ClosedRange<Int>) -> String {
        let start = index(startIndex, offsetBy: max(r.lowerBound, 0))
        let end = index(startIndex, offsetBy: min(r.upperBound, count - 1))
        return String(self[start...end])
    }
    /// 根据range获取字符串 a[0..<2]
    subscript(r: Range<Int>) -> String {
        let start = index(startIndex, offsetBy: max(r.lowerBound, 0))
        let end = index(startIndex, offsetBy: min(r.upperBound, count))
        return String(self[start..<end])
    }
    /// 根据range获取字符串 a[...2]
    subscript(r: PartialRangeThrough<Int>) -> String {
        let end = index(startIndex, offsetBy: min(r.upperBound, count - 1))
        return String(self[startIndex...end])
    }
    /// 根据range获取字符串 a[0...]
    subscript(r: PartialRangeFrom<Int>) -> String {
        let start = index(startIndex, offsetBy: max(r.lowerBound, 0))
        let end = index(startIndex, offsetBy: count - 1)
        return String(self[start...end])
    }
    /// 根据range获取字符串 a[..<3]
    subscript(r: PartialRangeUpTo<Int>) -> String {
        let end = index(startIndex, offsetBy: min(r.upperBound, count))
        return String(self[startIndex..<end])
    }
    /// 截取字符串: index 开始到结尾
    /// - Parameter index: 开始截取的index
    /// - Returns: string
    func subString(_ index: Int) -> String {
        guard index < count else {
            return ""
        }
        let start = self.index(endIndex, offsetBy: index - count)
        return String(self[start..<endIndex])
    }
    
    /// 截取字符串
    /// - Parameters:
    ///   - begin: 开始截取的索引
    ///   - count: 需要截取的个数
    /// - Returns: 字符串
    func substring(start: Int, _ count: Int) -> String {
        let begin = index(startIndex, offsetBy: max(0, start))
        let end = index(startIndex, offsetBy: min(count, start + count))
        return String(self[begin..<end])
    }

使用

        let str = "0123456789"
        print(str[of: 9])
        print(str[0...9])
        print(str[0..<9])
        
        print(str[0...])
        print(str[1...])
        print(str[...9])
        print(str[..<10])

相关文章

网友评论

    本文标题:Swift Range和字符串截取

    本文链接:https://www.haomeiwen.com/subject/iyzwyktx.html