I have a string for example \"ab ad adk fda kla kad ab ab kd\". I want to get all range of ab.(Here ab is present at 3 position so I should get 3 range).In normal scenarion
Swift 5:
The improved version of the most popular answer:
extension String {
func ranges(of substring: String, options: CompareOptions = [], locale: Locale? = nil) -> [Range] {
var ranges: [Range] = []
while let range = range(of: substring, options: options, range: (ranges.last?.upperBound ?? self.startIndex)..