gpt4 book ai didi

json - 更改其中包含链接的字符串

转载 作者:行者123 更新时间:2023-11-28 08:07:31 32 4
gpt4 key购买 nike

let profile_pic_url_hd = user["profile_pic_url_hd"] as! String
self.imgURL = "\(profile_pic_url_hd)"

self.imgURL 是一个链接,它是一个字符串。例如,链接是:

https://scontent-frx5-1.cdninstagram.com/t51.2885-19/s320x320/19121160_1328742810566926_6482637033138290688_a.jpg

有谁知道如何将此链接更改为:

https://scontent-frx5-1.cdninstagram.com/t51.2885-19/19121160_1328742810566926_6482637033138290688_a.jpg

换句话说,没有/s320x320/

最佳答案

您可以使用 URLComponents,拆分 URL 的路径,并重建一个新的 URL。

let urlstr = "https://scontent-frx5-1.cdninstagram.com/t51.2885-19/s320x320/19121160_1328742810566926_6482637033138290688_a.jpg"
if var comps = URLComponents(string: urlstr) {
var path = comps.path
var pathComps = path.components(separatedBy: "/")
pathComps.remove(at: 2) // this removes the s320x320
path = pathComps.joined(separator: "/")
comps.path = path
if let newStr = comps.string {
print(newStr)
}
}

输出:

https://scontent-frx5-1.cdninstagram.com/t51.2885-19/19121160_1328742810566926_6482637033138290688_a.jpg

关于json - 更改其中包含链接的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687172/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com