How to remove whitespaces in strings in Swift?

string, swift, whitespace

Solution

For Swift 5:

" spaces here ".replacingOccurrences(of: " ", with: "")

returns:

"spaceshere"

Problem

This works ``` let replaced = String(map(aString.generate()) { $0 == " " ? "-" : $0 }) ``` and this doesn't ``` let replaced = String(map(aString.generate()) { $0 == " " ? "" : $0 }) ``` Why?

Original source

Related problems