Skip to content

Instantly share code, notes, and snippets.

@vibinnair
Created August 5, 2019 07:51
Show Gist options
  • Save vibinnair/51e95bdbe180af2d07f3ceca522250d7 to your computer and use it in GitHub Desktop.
Save vibinnair/51e95bdbe180af2d07f3ceca522250d7 to your computer and use it in GitHub Desktop.
Functional Swift: Retreive all hashtags from a string
//Ref: https://www.swiftbysundell.com/basics/map-flatmap-and-compactmap
import Foundation
func hashTags(from string: String) -> [String] {
return string.components(separatedBy: .whitespacesAndNewlines)
.filter {string in string.starts(with: "#")}
}
print(hashTags(from: "#WWDC 2019 was #awesome #greatday"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment