【发布时间】:2018-08-10 01:16:03
【问题描述】:
我目前正在尝试设置一个要添加到 HTTP POST 请求的字符串,用户可以在其中键入文本并点击“enter”并发送请求。
我知道多个字符 (^,+,) 可以替换为单个字符 ('_'),如下所示:
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")
有没有更有效的方法?
【问题讨论】:
标签: swift character-replacement