编辑/更新:
Xpre 11•Swift 5.1或更高版本
import Foundationextension StringProtocol { func index<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? { range(of: string, options: options)?.lowerBound } func endIndex<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? { range(of: string, options: options)?.upperBound } func indices<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Index] { var indices: [Index] = [] var startIndex = self.startIndex while startIndex < endIndex, let range = self[startIndex...] .range(of: string, options: options) { indices.append(range.lowerBound) startIndex = range.lowerBound < range.upperBound ? range.upperBound : index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex } return indices } func ranges<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Range<Index>] { var result: [Range<Index>] = [] var startIndex = self.startIndex while startIndex < endIndex, let range = self[startIndex...] .range(of: string, options: options) { result.append(range) startIndex = range.lowerBound < range.upperBound ? range.upperBound : index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex } return result }}用法:
let str = "abcde"if let index = str.index(of: "cd") { let substring = str[..<index] // ab let string = String(substring) print(string) // "abn"}let str = "Hello, playground, playground, playground"str.index(of: "play") // 7str.endIndex(of: "play") // 11str.indices(of: "play") // [7, 19, 31]str.ranges(of: "play") // [{lowerBound 7, upperBound 11}, {lowerBound 19, upperBound 23}, {lowerBound 31, upperBound 35}]不区分大小写的样本
let query = "Play"let ranges = str.ranges(of: query, options: .caseInsensitive)let matches = ranges.map { str[$0] } //print(matches) // ["play", "play", "play"]正则表达式样本
let query = "play"let escapedQuery = NSRegularexpression.escapedPattern(for: query)let pattern = "\b(escapedQuery)\w+" // matches any word that starts with "play" prefixlet ranges = str.ranges(of: pattern, options: .regularexpression)let matches = ranges.map { str[$0] }print(matches) // ["playground", "playground", "playground"]


