This might be a duplicate. I couldn't find the answer in Swift, so I am not sure.
componentsSeparatedByCharactersInSet
removes the delimiter. If you separate by only one possible character it is easy to add it back. But what when you have a set?
Is there another method to split?
Swift 3 and 4 Versions
Thanks to Oisdk for getting me thinking.
This method works on
CollectionTypes
, rather thanString
s, but it should be easy enough to adapt:You could use it like this:
Or, this version, which uses a for-loop, and splits after the delimiter:
Or, if you wanted to get the most Swift features into one function (
defer
,throws
, a Protocol extension, an evilflatMap
,guard
, and Optionals):