【问题标题】:Convert String With Special Symbols like (|, &, -) into Url in Swift [duplicate]在 Swift 中将带有特殊符号(如(|、&、-))的字符串转换为 Url [重复]
【发布时间】:2021-07-17 06:04:47
【问题描述】:

我遇到了将字符串转换为 url 的问题。问题是包含一些特殊符号的字符串,如 &、| , -, # 等。我尝试对其使用不同的编码方法,但无法将其转换为 url。我尝试对返回有效字符串的字符串进行编码,例如转换为 %a 等的符号,但是当我再次尝试将此编码字符串转换为 url 时,得到相同的字符串(带有特殊字符)作为回报。

我分享了一个示例字符串。请让我知道我可以使用什么编码或转换方法将此字符串转换为 url。

示例字符串: http://qc.singlevendor.4livedemo.com/yokart/product/image/176/176|11|133|/330-440-webp?t=1623042841

注意:如果我在浏览器中粘贴相同的 url,它会在 url 上应用编码方法并打开正确的图像

提前致谢

【问题讨论】:

标签: ios swift swift5 urlencode


【解决方案1】:

你可以试试这个-

let input = "http://qc.singlevendor.4livedemo.com/yokart/product/image/176/176|11|133|/330-440-webp?t=1623042841"

let output = input.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print(output)

let expected = "http://qc.singlevendor.4livedemo.com/yokart/product/image/176/176%7C11%7C133%7C/330-440-webp?t=1623042841"
print(output == expected)

【讨论】:

    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2013-03-08
    • 1970-01-01
    • 2016-01-30
    • 2021-10-18
    相关资源
    最近更新 更多