I am looking for a way to replace characters in a Swift String
.
Example: \"This is my string\"
I would like to replace \" \" with \"+\" to get \
If you don't want to use the Objective-C NSString
methods, you can just use split
and join
:
var string = "This is my string"
string = join("+", split(string, isSeparator: { $0 == " " }))
split(string, isSeparator: { $0 == " " })
returns an array of strings (["This", "is", "my", "string"]
).
join
joins these elements with a +
, resulting in the desired output: "This+is+my+string"
.