编码
let title = "你好"
let utf1 = title.addingPercentEscapes(using: .utf8)
let utf2 = title.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)
print("utf1:\(utf1),+++utf2:\(utf2)")
utf1:Optional("%E4%BD%A0%E5%A5%BD"),+++utf2:Optional("%E4%BD%A0%E5%A5%BD")
解码
let str = utf1?.replacingPercentEscapes(using: .utf8)
let str2 = utf2?.removingPercentEncoding
DLog("str:\(str), str2:\(str2)")
str:Optional("你好"), str2:Optional("你好")
本文介绍了使用Swift语言进行字符串的编码与解码操作的方法。包括如何将字符串转换为URL安全的格式,并演示了如何从编码后的字符串还原原始内容。
691

被折叠的 条评论
为什么被折叠?



