How to print double quotes inside ""?
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:
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.
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)"#
According to your needs, you may use one of the 4 following patterns in order to print a Swift String
that contains double quotes in it.
1. Using escaped double quotation marks
String literals can include special characters such as \"
:
let string = "A string with \"double quotes\" in it."
print(string) //prints: A string with "double quotes" in it.
2. Using Unicode scalars
String literals can include Unicode scalar value written as \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. Using multiline string literals (requires Swift 4)
The The Swift Programming Language / Strings and Characters states:
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. Using raw string literals (requires Swift 5)
The The Swift Programming Language / Strings and Characters states:
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.
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)
- 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
which means that apart from being able to escape the character with backslash, you can use the unicode value. Following two statements are equivalent:
let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"
myString
would now contain:
I love "unnecessary" quotation marks