(swift) remove “\” character from string?

末鹿安然 提交于 2019-12-13 17:06:07

问题


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.


回答1:


var str = "\\dagdahughuad\\dajughdaug"
var str2 = str.stringByReplacingOccurrencesOfString("\\", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
print(str2)

This will output

dagdahughuaddajughdaug



回答2:


Converted above code in to SWIFT 3

    var str = "\\dagdahughuad\\dajughdaug"
    var str2 = str.replacingOccurrences(of: "\\", with: "", options: 
    NSString.CompareOptions.literal, range: nil)
    print(str2)


来源:https://stackoverflow.com/questions/30183162/swift-remove-character-from-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!