Created
November 17, 2015 08:36
-
-
Save IanKeen/429bf37d6335b64e03da to your computer and use it in GitHub Desktop.
String extension to allow splitting a string by multiple components/tokens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func componentsSeparatedByStrings(separators: [String]) -> [String] { | |
return separators.reduce([self]) { result, separator in | |
return result.flatMap { $0.componentsSeparatedByString(separator) } | |
} | |
.map { $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) } | |
} | |
} | |
/* Usage */ | |
"hello.there/how?are!you".componentsSeparatedByStrings([".", "/", "?", "!"]) | |
/* Result: ["hello", "there", "how", "are", "you"] */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment