Anson Yao Anson Yao - 3 months ago 24
Swift Question

How to remove slash "/" in string in Swift

I have a string like "http://example.com/a/b/c". I want to transform the string to "httpexamplecomabc" in order to save it as the file name. I have tried

let result = str.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "//"))


and

let result = str.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "/"))


but neither works. Any Idea on how to remove "/"? Thanks

Answer

You can replace occurrence of "/ " to "" by using

let mm = "http://example.com/a/b/c"

let newString = mm.stringByReplacingOccurrencesOfString("/", withString: "")
print(newString) // http:example.comabc
Comments