我目前正在尝试设置一个要添加到 HTTP POST 请求的字符串,用户将在其中键入文本并点击“输入”并发送请求。
我知道多个字符 (^,+,<,>) 可以替换为单个字符 ('_'),如下所示:
userText.replacingOccurrences(of: "[^+<>]", with: "_"
我目前正在使用以下多个功能:
.replacingOccurrences(of: StringProtocol, with:StringProtocol)
像这样:
let addAddress = userText.replacingOccurrences(of: " ", with: "_").replacingOccurrences(of: ".", with: "%2E").replacingOccurrences(of: "-", with: "%2D").replacingOccurrences(of: "(", with: "%28").replacingOccurrences(of: ")", with: "%29").replacingOccurrences(of: ",", with: "%2C").replacingOccurrences(of: "&", with: "%26")
有没有更有效的方法来做到这一点?