Coltuxumab
Coltuxumab

Reputation: 787

Remove punctuation characters from string except for dash in Swift

I have some code to remove all punctuation characters:

mysentence.components(separatedBy: .punctuationCharacters).joined().components(separatedBy: " ")

Now, I am trying to add an exception for the dash character "-". In other words, I want to remove all punctuation EXCEPT for dashes.

It looks like in Java this can be done with replaceall using a regular expression (which admittedly I don't know how to use). How might I do this in Swift 4?

Upvotes: 3

Views: 2795

Answers (1)

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7123

Maybe you remove dash - from CharacterSet like:

var set = CharacterSet.punctuationCharacters
set.remove(charactersIn: "-")

Upvotes: 5

Related Questions