如何在“”中打印双引号? [英] How to print double quotes inside ""?
问题描述
有人可以告诉我如何用带双引号打印。
Can someone please tell me how can I print something in following way "with" double quotes.
双引号
推荐答案
要在字符串中插入双引号之前带有反斜杠:
With a backslash before the double quote you want to insert in the String:
let sentence = "They said \"It's okay\", didn't they?"
现在句子
是:
他们说没关系,不是吗?
They said "It's okay", didn't they?
它被称为转义字符:您使用的是字面值,不会被解释。
It's called "escaping" a character: you're using its literal value, it will not be interpreted.
使用Swift 4,您也可以选择在不需要转义的文字文本上使用
分隔符:
With Swift 4 you can alternatively choose to use the """
delimiter for literal text where there's no need to escape:
let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""
这给出了:
他们说没关系,不是吗?
是的,他们所说的是。 p>
They said "It's okay", didn't they?
Yes, "okay" is what they said.
使用Swift 5,您可以使用增强的定界符:
With Swift 5 you can use enhanced delimiters:
现在可以使用增强的定界符。在开头引号之前带有一个或多个数字符号(#)的字符串文字,除非反斜杠和双引号字符后接加号,否则将它们视为文字。相同的数字符号。使用增强的定界符可以避免使包含许多双引号或反斜杠字符以及额外转义符的字符串文字混乱。
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.
您的字符串现在可以表示为:
Your string now can be represented as:
let sentence = #"They said "It's okay", didn't they?"#
如果要将变量添加到字符串中,还应该添加#
反斜杠后:
And if you want add variable to your string you should also add #
after backslash:
let sentence = #"My "homepage" is \#(url)"#
这篇关于如何在“”中打印双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!