如何从 url 中删除特殊字符?
How to remove special characters from url?
let ids = [String: [String]]
ids=%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D
附加并传递 url 参数后,在响应结构特殊符号中添加的响应失败 -> %5B%%
https://baseUrl/endpoint?ids=4566abef1c-4462-4g62-bcc5-5ae10547104c,1256efcf8c-6977-430d-b3ec-4ae80547101c
如何从 url 中删除 %22%5D?
此处代码:
let parms: [String: [String]]
let urlString = "\(baseUrl)/\(endpoint)"
Connector.requestSwiftyJson(
url: urlString,
requestType: .get,
withParams: parms,
loader: false
) { json, response, error in
您可以通过在将参数传递给解析器之前调整参数来删除不需要的字符,例如:
let adaptedParams = params.reduce(into: [String: String]()) { [=10=][.key] = .value.joined(separator: ",") }
“...如何从 url 中删除 %22%5D
?”。试试这个:
let ids = "%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D"
let cleanUrl = ids.replacingOccurrences(of: "%22%5D", with: "")
print("\n---> cleanUrl: \(cleanUrl) \n")
您还可以使用以下方法删除所有百分比编码:
if let cleanUrl = ids.replacingOccurrences(of: "%5B", with: "") // [
.replacingOccurrences(of: "%5D", with: "") // ]
.replacingOccurrences(of: "%22", with: "") // "
.removingPercentEncoding { // other encoding
print("\n---> cleanUrl: \(cleanUrl) \n")
}
有一个API删除百分比编码
let string = "ids=%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D"
let cleanedString = string.removingPercentEncoding
但是,如果您需要提取 UUID,可以使用正则表达式来完成
func extractUUID(from string : String) -> [String]
{
let pattern = "[0-9a-f]{10}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: string, range: NSRange(string.startIndex..., in: string))
return matches.map { match -> String in
let range = Range(match.range, in: string)!
return String(string[range])
}
}
let uuids = extractUUID(from: "ids=%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D")
print(uuids)
注意:第一个UUID中的g
是无效字符
let ids = [String: [String]]
ids=%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D
附加并传递 url 参数后,在响应结构特殊符号中添加的响应失败 -> %5B%%
https://baseUrl/endpoint?ids=4566abef1c-4462-4g62-bcc5-5ae10547104c,1256efcf8c-6977-430d-b3ec-4ae80547101c
如何从 url 中删除 %22%5D?
此处代码:
let parms: [String: [String]]
let urlString = "\(baseUrl)/\(endpoint)"
Connector.requestSwiftyJson(
url: urlString,
requestType: .get,
withParams: parms,
loader: false
) { json, response, error in
您可以通过在将参数传递给解析器之前调整参数来删除不需要的字符,例如:
let adaptedParams = params.reduce(into: [String: String]()) { [=10=][.key] = .value.joined(separator: ",") }
“...如何从 url 中删除 %22%5D
?”。试试这个:
let ids = "%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D"
let cleanUrl = ids.replacingOccurrences(of: "%22%5D", with: "")
print("\n---> cleanUrl: \(cleanUrl) \n")
您还可以使用以下方法删除所有百分比编码:
if let cleanUrl = ids.replacingOccurrences(of: "%5B", with: "") // [
.replacingOccurrences(of: "%5D", with: "") // ]
.replacingOccurrences(of: "%22", with: "") // "
.removingPercentEncoding { // other encoding
print("\n---> cleanUrl: \(cleanUrl) \n")
}
有一个API删除百分比编码
let string = "ids=%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D"
let cleanedString = string.removingPercentEncoding
但是,如果您需要提取 UUID,可以使用正则表达式来完成
func extractUUID(from string : String) -> [String]
{
let pattern = "[0-9a-f]{10}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: string, range: NSRange(string.startIndex..., in: string))
return matches.map { match -> String in
let range = Range(match.range, in: string)!
return String(string[range])
}
}
let uuids = extractUUID(from: "ids=%5B%4566abef1c-4462-4g62-bcc5-5ae10547104c%22,%20%1256efcf8c-6977-430d-b3ec-4ae80547101c%22%5D")
print(uuids)
注意:第一个UUID中的g
是无效字符