How to find all positions of one string in another string in swift2 ?

后端 未结 4 1397
执念已碎
执念已碎 2021-01-02 06:12

I can find first position of string \"ATG\" in myString \"ATGGACGTGAGCTGATCGATGGCTGAAATGAAAA\" (i.e. index range is 0..<3) by using code below. Question is how to find al

4条回答
  •  没有蜡笔的小新
    2021-01-02 06:29

    extension String {
        public func rangesOfString(searchString:String, options: NSStringCompareOptions = [], searchRange:Range? = nil ) -> [Range] {
            if let range = rangeOfString(searchString, options: options, range:searchRange) {
    
                let nextRange = Range(start:range.endIndex, end:self.endIndex)
                return [range] + rangesOfString(searchString, searchRange: nextRange)
            } else {
                return []
            }
        }
    }
    

提交回复
热议问题