如何在“”内打印双引号?

时间:2022-09-15 16:26:12

Can someone please tell me how can I print something in following way "with" double quotes.

谁能告诉我怎样用“双引号”来打印东西吗?

"Double Quotes"

“双引号”

2 个解决方案

#1


121  

With a backslash before the double quote you want to insert in the String:

在你想要插入的双引号之前加上一个反斜杠:

let sentence = "They said \"It's okay\", didn't they?"

Now sentence is:

现在的句子是:

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.

它被称为“转义”:你正在使用它的文字值,它不会被解释。


With Swift 4 you can alternatively choose to use the """ delimiter for literal text where there's no need to escape:

使用Swift 4,您可以选择在不需要转义的文本中使用“”分隔符:

let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""

This gives:

这给:

They said "It's okay", didn't they?
Yes, "okay" is what they said.

他们说“没事”,不是吗?是的,他们是这么说的。

#2


18  

For completeness, from Apple docs:

为了完整性,从苹果文档:

String literals can include the following special characters:

字符串字面值可以包括以下特殊字符:

  • The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
  • 转义特殊字符\0(空字符)\(反斜杠)\t(水平标签)\n(换行)\r(回车)\"(双引号)\'(单引号)\
  • An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
  • 一个任意的Unicode标量,写成\u{n},其中n是一个1-8位的十六进制数字,值等于一个有效的Unicode代码点

which means that apart from being able to escape the character with backslash, you can use the unicode value. Following two statements are equivalent:

这意味着除了可以用反斜杠转义字符之外,还可以使用unicode值。以下两种说法是等价的:

let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"

myString would now contain:

现在myString将包含:

I love "unnecessary" quotation marks

我喜欢“不必要的”引号

#1


121  

With a backslash before the double quote you want to insert in the String:

在你想要插入的双引号之前加上一个反斜杠:

let sentence = "They said \"It's okay\", didn't they?"

Now sentence is:

现在的句子是:

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.

它被称为“转义”:你正在使用它的文字值,它不会被解释。


With Swift 4 you can alternatively choose to use the """ delimiter for literal text where there's no need to escape:

使用Swift 4,您可以选择在不需要转义的文本中使用“”分隔符:

let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""

This gives:

这给:

They said "It's okay", didn't they?
Yes, "okay" is what they said.

他们说“没事”,不是吗?是的,他们是这么说的。

#2


18  

For completeness, from Apple docs:

为了完整性,从苹果文档:

String literals can include the following special characters:

字符串字面值可以包括以下特殊字符:

  • The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
  • 转义特殊字符\0(空字符)\(反斜杠)\t(水平标签)\n(换行)\r(回车)\"(双引号)\'(单引号)\
  • An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
  • 一个任意的Unicode标量,写成\u{n},其中n是一个1-8位的十六进制数字,值等于一个有效的Unicode代码点

which means that apart from being able to escape the character with backslash, you can use the unicode value. Following two statements are equivalent:

这意味着除了可以用反斜杠转义字符之外,还可以使用unicode值。以下两种说法是等价的:

let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"

myString would now contain:

现在myString将包含:

I love "unnecessary" quotation marks

我喜欢“不必要的”引号