Swift 5.1 字符串截取

Swift4.0以后字符串截取发生了变化,为了使用起来方便,创建了一个String的扩展实现分类效果


extension String{
    
    /// 从某个位置开始截取:
    /// - Parameter index: 起始位置
    public func substring(from index: Int) -> String {
        if(self.count > index){
            let startIndex = self.index(self.startIndex,offsetBy: index)
            let subString = self[startIndex.. String {
        if(self.count > index){
            let endindex = self.index(self.startIndex, offsetBy: index)
            let subString = self[self.startIndex.. String{
        var string = String()
        if(rangs.location >= 0) && (self.count > (rangs.location + rangs.length)){
            let startIndex = self.index(self.startIndex,offsetBy: rangs.location)
            let endIndex = self.index(self.startIndex,offsetBy: (rangs.location + rangs.length))
            let subString = self[startIndex..

你可能感兴趣的:(Swift 5.1 字符串截取)