Different ways to Escape Double Quotes in C# Programming:
Example # 1: Using @ and “ ” Escape Character.
In this example, we use @ and “ “ escape characters for escaping double quotes in C# programming. We use the Ubuntu 20.04 text editor for performing these examples. We save our C# code file with the “.cs” file extension. Let’s try this given example to learn this concept.
The starting statement of the C# program is the “using System”. When we write this line, we will be using the system library in our C# code. It contains different classes and functions. We will get access to the “Console” class and the “WriteLine()” function when we use this library. After importing this library, we have a “namespace”. The name of this namespace is “CsharpProgram”. After this, we declare a class with the name “Demo”.
We initialize the “main” function which is “static” here and declare a variable with the name “message” of string data type. We initialize this “message” variable with the string which is “My First Csharp Program”. To escape double quotes on “Csharp”, we use the @ symbol at the start of the string and place the double quotes inside the other pair of the double quote (like this “Csharp”). You must put @ symbol before this string. After this, we have to print the line with escape quotes on the terminal screen. So, we use the “Console.WriteLine” function which renders this line on the terminal.
For the execution of this code, we have different commands. As we use the Ubuntu 20.04 server, we must follow the commands for getting the output. We have two commands here in the given image. First, we have to use the “mcs” compiler which compiles this C# code. After the successful compilation, this will create an executable file for us. Then we use the “.exe” file extension with the “mono” command and this command executes this code. We get the output when both commands run successfully. In the image below, the “CSharp” is displayed inside the double quotes.
Example # 2: Using \ Backslash Escape Character.
We are going to elaborate on another example in which we use the second method for escaping double quotes in C# programming in Ubuntu 20.04. Here, we use the backslash escape character for escaping double-quotes:
We initialize the “System” namespace on the first line of the C# code. We are going to utilize this “using System” for getting basic functions and classes of the C# program. Then, we also have a namespace named “Program”. We have to start the class with the name “Csharp” and invoke the “main” method. Within this “main” method, we declare a string variable “msg” and store a string. In the string, we are going to put the double quotes.
So, for this, we use the backslash method inside this string data. We place this \ at the start and end of the string where we want to put the double quotes. In this example, we have to put the double quotes on the string “Escape Quotes”. So, we write this string as “\Escape Quotes\”. We should write a \ before each double quote. You must be sure that you are using the correct syntax of the backslash escape character. The string “Escape Quotes” is enclosed in the double quotation mark.
Now, we are going to use the “WriteLine” function of the “Console” class so that we can print this string on the display screen. We also paste the screenshot of the output of the programs so you can easily understand how these methods escape double quotes in C# programming. Here, you can see that the string “Escape Quotes” is written inside the double-quotes.
Example # 3:
Let’s explore the last example in which we use both methods in a single C# program. We explain both methods for escaping quotes in C# separately. Now, we are going to use both methods in one program for escaping quotes.
Start the program with the same “using System” library which we discuss in examples 1 and 2. The namespace here is named “Escape_Quotes” and declares a class in this C# program with the name “DemoProgram”. The “main” function is the compulsory part of our C# program because it is the entry point of our C# program. Here, in the code, we are using two string variables. So, we declare one string variable with the name “Fruits” and the other string variable with the name “Vegetables”.
We also store some fruit names to the “string Fruits” and some vegetable names to the second string “Vegetables”. Here, we have to escape quotes on each name of fruit and vegetable. For escaping quotes on fruit names, we use the second method which is the \ backslash method. We are using the same technique as we explained above in example 2. We have to write the backslash before each double-quote. We write this as “\” Apple\” \” Mango\” \”Orange\” \”Banana\”” and store this string as the first string “Fruits”.
So, all the names of fruits will be surrounded by double quotes. We also put double-quotes on each vegetable name so for this. We use the first method which we discuss in example 1. First, we put the @ symbol and then put the double quotes inside the other pair of the double quotes as shown in the above image. Then, we store these vegetable names in the second string “Vegetables”. Now, it’s time to print these strings. The WriteLine function of the Console class will display these names on the screen. Here, we use four “Console.WriteLine” statements to show the output. We are going to paste the screenshot of the output below:
The output shows that each name of the fruit and vegetable is surrounded by the double-quotes. In the code above, we use two alternative ways for escaping quotes in C# programming. We apply the backslash method on fruit names and @ “” method on vegetable names but the output of both methods is the same.
This article helps you learn escape quotes in C# programming. We have defined the methods for escaping quotes and then explored different examples in which we have used different methods for adding double quotes. Here, we have explained two methods for escaping quotes and performed all these examples on Ubuntu 20.04. We also provided the screenshots of the code and the output.