(swift) remove "\" character from string?

Rahul Sonvane picture Rahul Sonvane · May 12, 2015 · Viewed 14.6k times · Source

I have string which contains few \ and I want to remove all of them. can anyone tell me how can I do that. I have already tried stringByReplacingOccurrencesOfString but it's not working out for me.

Answer

Leo picture Leo · May 12, 2015
var str = "\\dagdahughuad\\dajughdaug"
var str2 = str.stringByReplacingOccurrencesOfString("\\", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
print(str2)

This will output

dagdahughuaddajughdaug