I\'m used to do this in JavaScript:
var domains = \"abcde\".substring(0, \"abcde\".indexOf(\"cd\")) // Returns \"ab\"
Swift doesn\'t have t
Leo Dabus's answer is great. Here is my answer based on his answer using compactMap
to avoid Index out of range
error.
extension StringProtocol {
func ranges(of targetString: Self, options: String.CompareOptions = [], locale: Locale? = nil) -> [Range] {
let result: [Range] = self.indices.compactMap { startIndex in
let targetStringEndIndex = index(startIndex, offsetBy: targetString.count, limitedBy: endIndex) ?? endIndex
return range(of: targetString, options: options, range: startIndex..