Swift Program to demonstrate the example to write double-quotes in a string


In Swift, a string is a sequence of characters that represent in between double quotes, for example: ”Learn Swift”, “tutorialspoint”, etc. But when you print the string double quotes were removed by the compiler and you will get Learn Swift, tutorialspoint in the output.

So if you want to print double quotes in the output, then you have to place the backslash character or escape character(\) before the double quotes you want to print inside the given string. It tells the compiler that the character should be treated as a literal character, it is not part of string syntax. The backslash before a double quote is also known as a double quote character(\”).

Example

Input: "Today is "Raining""
Output: Today is "Raining"

Here, the escape character is present before double quotes so the double quotes are treated as literal, not as part of syntax.

Example 1

In the following Swift program, we will demonstrate how to write double quotes in a string. So for that, we are going to use an escape character before the double quote.

import Foundation
import Glibc

let myStr1 = "Birds are playing in the \"rain"
print("String 1:", myStr1)

let myStr2 = "Ram is good \"IOS developer\""
print("String 2:", myStr2)

let myStr3 = "Sita uses double quotes(\"\")"
print("String 3:", myStr3)

Output

String 1: Birds are playing in the "rain
String 2: Ram is good "IOS developer"
String 3: Sita uses double quotes("")

Example 2

In the following Swift program, we will demonstrate how to write double quotes in a string. So while working with multi-lines we don't need to use double quote character(\”). We just simply use double quotes on the word which we want to highlight and the compiler will display the result without any error.

import Foundation
import Glibc

let InputString = """
Hello Tow
Do you like to play "Football"?
No i dont like "Football"

"""

print(InputString)

Output

Hello Tow
Do you like to play "Football"?
No i dont like "Football"

Conclusion

So this is how we can write double quotes in a string. Using backslash character(\) we only display one double quote at a time if you want to display multiple double quotes, then you have to use backslash before each quote, e.g., \”\”\”. You can use double quotes when you want to highlight some specific word in the output string. If you want to print a single quote, then you can use (\’).

Updated on: 14-Jun-2023

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements