c# - How to escape double quotes in a string

String escape double quote
The following asp.net c# example code demonstrate us how can we escape double quote in a string programmatically at run time in an asp.net application. .Net framework's String Class represent text as a series of Unicode characters. We can escape double quotes (") in a string by several ways.

We can escape double quotes in a string by using a escape character Backslash (\). If we want to include a double quotes in this way, we should write the string as ("a \"sample\" text"). Here the word (sample) will be surrounded by two double quotes as "sample".

We also can escape double quotes in a string object by using @ symbol. If we want to write the same string in this technique, then we should format it as (@"a ""sample"" text"). Here we also need to place a double quotes two times to get output a single double quotes. Both techniques show the same output.

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create string variable with double quote.
        string stringPlants = "\"Brown Betty\" \"Meadow Cabbage\" \"Swamp Cabbage\"";

        //another way to escape double quote in string
        string stringPlants2 = @"""California Sycamore"" ""California Walnut"" ""Canada Root""";
        Label1.Text = "string of plants..................<br />";
        Label1.Text += stringPlants;
        Label1.Text += "<br />"+stringPlants2;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string escape double quote</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string escape double quote
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string escape double quote"  
More c# examples