将引号添加到字符串中

时间:2016-12-21 13:06:56

标签: string swift3 concatenation

我想连接到另一个包含“'”字符的字符串 字符串看起来像想要但是当我用这个字符串创建一个Object时,我在里面得到一个“\”:

我签入https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID285如何处理特殊字符

编辑: 我在理解问题根源时犯了一个错误...... 当我尝试使用URLQueryItem构建URL时,我得到了

print(EventToUrl.name) // print John's Event
let name = URLQueryItem(name: "name", value: EventToUrl.name)                               
print(name.description) //print name=John's Event
print(name.value) // print Optional("John\'s Event")
deepLink.queryItems = [name]
print(deepLink.url) //print Optional(https://www.example.com/?id%3D-KZXZG2bcuKHNdYNcGTF%26name%3DJohn's%2520Event%26....)

当我尝试发送此深层链接时,由于“'”字符

,我收到错误

我也在这里尝试Eric Aya的回答: How to use special character in NSURL? 但那不适合我:

print(EventToUrl.name)  //print "John's Event"
let name = URLQueryItem(name: "name", value: EventToUrl.name.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed))
print(name.description) //Print name=John's%20Event
print(name.value) //Print Optional("John\'s%20Event")
print(deepLink.url)    //print
    Optional(https://www.example.com/?id%3D-KZXWVbBolmso5DT4p2I%26name%3John's%252520Event%26.......)

自包含代码:

var urlTestComponents = URLComponents()
urlTestComponents.scheme = "https"
urlTestComponents.host = "www.example.com"
urlTestComponents.path = "/"
let nameTest = URLQueryItem(name: "name", value: "John's Event")
urlTestComponents.queryItems = [name]
print(urlTestComponents.url)

最后一行打印“https://www.example.com/?name=jeremie的%20Event”并且如果我尝试发送(使用whatsApp),则因为“'”而在中间剪切网址

url cut

0 个答案:

没有答案