如何更改链接内的随机字符串?

时间:2019-02-27 06:27:35

标签: ios swift string

我正在尝试从链接中删除一些随机值字符串。但是我做不到。我该怎么做呢?

有我的代码:

self.baslikLinki.replacingOccurrences(of: "applewebdata://[0-9A-Z-]*/?", with: "category/user/", options: .literal, range: nil)

链接看起来像:

applewebdata://DA4343F3-A092-4BF1-B76F-7FC7F128C7D9/?q=se%c3%a7im+yasaklar%c4%b1

2 个答案:

答案 0 :(得分:0)

只需解析URL并用所需的字符串替换主机和路径!

let link = "applewebdata://DA4343F3-A092-4BF1-B76F-7FC7F128C7D9/?q=se%c3%a7im+yasaklar%c4%b1"
var url = URLComponents(string: link)!
url.host = "category"
url.path = "/user/"
print("\(url)") // applewebdata://category/user/?q=se%c3%a7im+yasaklar%c4%b1

答案 1 :(得分:-2)

简单更换:

 let originalString= "xyzABC"
 let replaced = originalString.replacingOccurrences(of: "ABC", with: "whatever")

对于正则表达式:

var originalString = "!hi !!"
let pattern = "!h%"
originalString = originalString.replacingOccurrences(of: pattern, with: " ", options: .regularExpression)