有人可以告诉我如何以“带”双引号的方式打印某些内容。
“双引号”
最佳答案
在要插入字符串的双引号之前加一个反斜杠:
let sentence = "They said \"It's okay\", didn't they?"
现在句子
是:
They said "It's okay", didn't they?
这称为“转义”字符:您正在使用它的文字值,它不会被解释。
<小时/>使用 Swift 4,您也可以选择对不需要转义的文字文本使用 """
分隔符:
let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""
这给出:
<小时/>They said "It's okay", didn't they?
Yes, "okay" is what they said.
在 Swift 5 中,您可以使用增强的分隔符:
String literals can now be expressed using enhanced delimiters. A string literal with one or more number signs (#) before the opening quote treats backslashes and double-quote characters as literal unless they’re followed by the same number of number signs. Use enhanced delimiters to avoid cluttering string literals that contain many double-quote or backslash characters with extra escapes.
您的字符串现在可以表示为:
let sentence = #"They said "It's okay", didn't they?"#
如果你想在字符串中添加变量,你还应该在反斜杠后添加#
:
let sentence = #"My "homepage" is \#(url)"#
关于swift - 如何在 ""内打印双引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37223429/