1

How to remove any special characters in a string? Example: , or in string with Swift 3.

I try to remove them with:

.replacingOccurrences(of: "\\u{+}", with: "", options: .regularExpression, range: nil) 

but it didn't work.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Possible duplicate of [Swift - Replacing emojis in a string with whitespace](https://stackoverflow.com/questions/36919125/swift-replacing-emojis-in-a-string-with-whitespace) – garrettmurray Sep 22 '17 at 03:52
  • 2
    What do you mean exactly by "special characters"? Just Emoji characters? What about math symbols? What about Cyrillic? Or Arabic? Or Chinese? Or anything else? Update your question with actual code showing real strings you wish to update. Show the desired results and your actual results. – rmaddy Sep 22 '17 at 04:13

1 Answers1

4

Working in Swift 4.1

import Foundation

extension String {
    var withoutSpecialCharacters: String {
        return self.components(separatedBy: CharacterSet.symbols).joined(separator: "")
    }
}
Ryan
  • 512
  • 1
  • 5
  • 17