如何删除,而不是使用Swift解码字符串中的百分比转义字符。例如:
"hello%20there"
应该成为
"hellothere"
编辑:
我想在字符串中替换多个转义百分比的字符。所以:
"hello%20there%0Dperson"
应该成为
"hellothereperson"
答案 0 :(得分:6)
class MessagesManager {
constructor() {
this.msgs = (localStorage.getItem("messages"))?JSON.parse(localStorage.getItem("messages")):[];
}
add(author, message) {
var msg = new Message(author, message)
this.msgs.push(msg);
}
save() {
localStorage.setItem("messages", JSON.stringify(this.msgs));
}
delete() {
this.msgs = [];
localStorage.removeItem("messages");
}
答案 1 :(得分:1)
您可以使用正则表达式匹配%
后跟两个数字:%[0-9a-fA-F]{2}
let myString = "hello%20there%0D%24person"
let regex = try! NSRegularExpression(pattern: "%[0-9a-fA-F]{2}", options: [])
let range = NSMakeRange(0, myString.characters.count)
let modString = regex.stringByReplacingMatchesInString(myString,
options: [],
range: range,
withTemplate: "")
print(modString)
答案 2 :(得分:1)
您可以使用“removingPercentEncoding”方法
let precentEncodedString = "hello%20there%0Dperson"
let decodedString = precentEncodedString.removingPercentEncoding ?? ""
答案 3 :(得分:0)
let input:String = "hello%20there%0Dperson"
guard let output = input.stringByRemovingPercentEncoding else{
NSLog("failed to remove percent encoding")
return
}
NSLog(output)
,结果是
hello there
person
然后你可以删除空格
或者您可以通过正则表达式将其删除
"%([0-9a-fA-F]{2})"