如何在“”内打印双引号?
How to print double quotes inside ""?
谁能告诉我如何按以下方式打印内容 "with" 双引号。
"Double Quotes"
在要插入字符串的双引号前加上反斜杠:
let sentence = "They said \"It's okay\", didn't they?"
现在 sentence
是:
They said "It's okay", didn't they?
它被称为"escaping"一个字符:您正在使用它的字面值,它不会被解释。
使用 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)"#
为了完整起见,来自Apple docs:
String literals can include the following special characters:
- The escaped special characters [=12=] (null character), \ (backslash), \t
(horizontal tab), \n (line feed), \r (carriage return), \" (double
quote) and \' (single quote)
- 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值。以下两个语句是等价的:
let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"
myString
现在将包含:
I love "unnecessary" quotation marks
根据您的需要,您可以使用以下 4 种模式之一 打印包含双引号的 Swift String
.
1。使用转义双引号
字符串文字可以包含特殊字符,例如 \"
:
let string = "A string with \"double quotes\" in it."
print(string) //prints: A string with "double quotes" in it.
2。使用 Unicode 标量
字符串文字可以包括写成 \u{n}
:
的 Unicode 标量值
let string = "A string with \u{22}double quotes\u{22} in it."
print(string) //prints: A string with "double quotes" in it.
3。使用多行字符串文字(需要 Swift 4)
The Swift Programming Language / Strings and Characters 状态:
Because multiline string literals use three double quotation marks instead of just one, you can include a double quotation mark ("
) inside of a multiline string literal without escaping it.
let string = """
A string with "double quotes" in it.
"""
print(string) //prints: A string with "double quotes" in it.
4。使用原始字符串文字(需要 Swift 5)
The Swift Programming Language / Strings and Characters 状态:
You can place a string literal within extended delimiters to include special characters in a string without invoking their effect. You place your string within quotation marks ("
) and surround that with number signs (#
). For example, printing the string literal #"Line 1\nLine 2"#
prints the line feed escape sequence (\n
) rather than printing the string across two lines.
let string = #"A string with "double quotes" in it."#
print(string) //prints: A string with "double quotes" in it.
谁能告诉我如何按以下方式打印内容 "with" 双引号。
"Double Quotes"
在要插入字符串的双引号前加上反斜杠:
let sentence = "They said \"It's okay\", didn't they?"
现在 sentence
是:
They said "It's okay", didn't they?
它被称为"escaping"一个字符:您正在使用它的字面值,它不会被解释。
使用 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)"#
为了完整起见,来自Apple docs:
String literals can include the following special characters:
- The escaped special characters [=12=] (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
- 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值。以下两个语句是等价的:
let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"
myString
现在将包含:
I love "unnecessary" quotation marks
根据您的需要,您可以使用以下 4 种模式之一 打印包含双引号的 Swift String
.
1。使用转义双引号
字符串文字可以包含特殊字符,例如 \"
:
let string = "A string with \"double quotes\" in it."
print(string) //prints: A string with "double quotes" in it.
2。使用 Unicode 标量
字符串文字可以包括写成 \u{n}
:
let string = "A string with \u{22}double quotes\u{22} in it."
print(string) //prints: A string with "double quotes" in it.
3。使用多行字符串文字(需要 Swift 4)
The Swift Programming Language / Strings and Characters 状态:
Because multiline string literals use three double quotation marks instead of just one, you can include a double quotation mark (
"
) inside of a multiline string literal without escaping it.
let string = """
A string with "double quotes" in it.
"""
print(string) //prints: A string with "double quotes" in it.
4。使用原始字符串文字(需要 Swift 5)
The Swift Programming Language / Strings and Characters 状态:
You can place a string literal within extended delimiters to include special characters in a string without invoking their effect. You place your string within quotation marks (
"
) and surround that with number signs (#
). For example, printing the string literal#"Line 1\nLine 2"#
prints the line feed escape sequence (\n
) rather than printing the string across two lines.
let string = #"A string with "double quotes" in it."#
print(string) //prints: A string with "double quotes" in it.