-2

I'd like to use CharacterSet.whitespacesAndNewlines to remove whitespace from a string.

However, the code below does not compile.

myString.replacingOccurrences(of: CharacterSet.whitespacesAndNewlines, with: "")
// Instance method 'replacingOccurrences(of:with:options:range:)' requires that 'CharacterSet' conform to 'StringProtocol'

What is a simple way to achieve the intended result?

Ilias Karim
  • 4,798
  • 3
  • 38
  • 60

1 Answers1

1

You can use

let result = myString.filter { !$0.isWhitespace }
David Ansermot
  • 6,052
  • 8
  • 47
  • 82