swift - 如何在 Swift 中删除字符串中的斜杠 "/"

标签 swift

我有一个类似“http://example.com/a/b/c”的字符串。我想将字符串转换为“httpexamplecomabc”,以便将其另存为文件名。我试过了

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

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

但两者都不起作用。关于如何删除“/”的任何想法?谢谢

最佳答案

您可以使用以下方法将出现的“/”替换为“”

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

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

关于swift - 如何在 Swift 中删除字符串中的斜杠 "/",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38887396/

相关文章:

swift - 将 MKMapRect.origin 转换为经度和纬度

ios - SIGABRT :Upload tasks from NSData are not supported in background sessions

ios - PDFKit PDFThumbnailView 缩略图不居中

ios - 如何解决 SceneKit double not supported 错误?

swift - 字符串数组排序

ios - 如何快速执行从 Root View Controller 到导航堆栈中最后一个 View Controller 的 segue?

ios - 如果平移,UIButton 框架不会改变

ios - 解除 UIViewController 时 UINavigationBar 奇怪的颜色变化动画

ios - Firebase 数据结构和过滤

ios - 禁用 UIContextualAction