Hacker News new | ask | show | jobs
by comex 3798 days ago
I'd do

    s = str.replacing(" ", with: " ")
which seems to go along well with the "sorted" and "appending" mentioned in the new API design guidelines:

https://swift.org/documentation/api-design-guidelines/#be-gr...

This is a bit more verbose if you need to chain a lot of replacements... but chances are you shouldn't be doing that in the first place! Instead there should be a version that takes a dictionary, like

    str.replacing(["<": "&lt;", "&": "&amp;"])
This is better because most of the time you don't actually want characters produced by earlier replacements to match later ones, and it can be faster for large strings.