Search code examples
c#asp.netdouble-quotes

How can I add double quotes to a string that is inside a variable?


I have a string variable such as this:

string title = string.empty;

I have to display the content of whatever is passed to it inside a div within double quotes. I have written something like this:

...
...
<div>"+ title +@"</div>
...
...

How can I add the double quotes here? So that it will display like:

"How to add double quotes"

Solution

  • You need to escape them by doubling them (verbatim string literal):

    string str = @"""How to add doublequotes""";
    

    Or with a normal string literal you escape them with a \:

    string str = "\"How to add doublequotes\"";
    

    Update:

    With C# 11, you can use raw string literals:

    string str = """
        "How to add doublequotes"
        """