Skip to content

Instantly share code, notes, and snippets.

@piyush23dez
Created August 3, 2019 23:46
Show Gist options
  • Save piyush23dez/a84743cb42115c2cedd90ee76ba3329f to your computer and use it in GitHub Desktop.
Save piyush23dez/a84743cb42115c2cedd90ee76ba3329f to your computer and use it in GitHub Desktop.
//https://medium.com/@sorenlind/three-ways-to-enumerate-the-words-in-a-string-using-swift-7da5504f0062
extension String {
func words() -> [String] {
let range = self.startIndex..<self.endIndex
var words = [String]()
self.enumerateSubstrings(in: range, options: NSString.EnumerationOptions.byWords) { (substring, _, _, _) -> () in
words.append(substring ?? "")
}
return words
}
}
var st = "ajdlkkl . d,"
print(st.words())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment