212

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"
jordanz
  • 367
  • 4
  • 12
ANP
  • 15,287
  • 22
  • 58
  • 79
  • Nope doesn't work asp.net, c# vs2013 no matter how i code if you look and the page source " and ' are always displayed. I'm using `Attributes.Add(...' – djack109 May 18 '16 at 17:50
  • this would be helpful : https://stackoverflow.com/questions/46298985/chr34-equivalent?noredirect=1&lq=1 – Siavash Sep 25 '18 at 10:42

20 Answers20

411

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\"";
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • and how to remove it while saving in database? I just want to remove additional double quote used for escaping and not both – Anil Purswani Sep 30 '14 at 10:35
  • 1
    @AnilPurswani - huh? You need to read up on what escaping means. – Oded Sep 30 '14 at 10:38
  • 1
    when used string str = @"""How to add doublequotes"""; it stores ""How to add doublequotes"" in database.....and while fetching it retrieves the same.... now trying to convert it....well I got the answer - str.Replace("\\\"", "\""); ...... anyways thanks for your reply .... – Anil Purswani Sep 30 '14 at 10:42
  • If you want a string to contain quotes, you escape them. That has nothing to do with storing them in the database - if you want to strip the quotes before storing in the DB, then do that. – Oded Sep 30 '14 at 10:43
  • yeah I was just trying to get the solution of http://stackoverflow.com/questions/26118354/xslcompiledtransform-load-raises-object-reference-not-set-to-an-instance-of-an-o and reached to this question, which seems similar when was the data fetched from database..... – Anil Purswani Sep 30 '14 at 10:49
  • Nope doesn't work asp.net, c# vs2013 no matter how i code if you look and the page source " and ' are always displayed. I'm using `Attributes.Add(...` – djack109 May 18 '16 at 17:49
  • @djack109 - that's a different issue. Perhaps ask a question about your exact issue? – Oded May 18 '16 at 18:00
  • @AnilPurswani the better way to remove all quotes would be: `.Trim('"')` – Flimtix Apr 05 '22 at 10:51
58

So you are essentially asking how to store doublequotes within a string variable? Two solutions for that:

var string1 = @"""inside quotes""";
var string2 = "\"inside quotes\"";

In order to perhaps make it a bit more clear what happens:

var string1 = @"before ""inside"" after";
var string2 = "before \"inside\" after";
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
23

If you have to do this often and you would like this to be cleaner in code, you might like to have an extension method for this.

This is really obvious code, but still I think it can be useful to grab it and make you save time.

  /// <summary>
    /// Put a string between double quotes.
    /// </summary>
    /// <param name="value">Value to be put between double quotes ex: foo</param>
    /// <returns>double quoted string ex: "foo"</returns>
    public static string AddDoubleQuotes(this string value)
    {
        return "\"" + value + "\"";
    }

Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codea
  • 1,439
  • 1
  • 17
  • 31
16

If I understand your question properly, maybe you can try this:

string title = string.Format("<div>\"{0}\"</div>", "some text");
7

Another note:

    string path = @"H:\\MOVIES\\Battel SHIP\\done-battleship-cd1.avi";
    string hh = string.Format("\"{0}\"", path);
    Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe ", hh + " ,--play");

The real value of hh, as passed, will be "H:\MOVIES\Battel SHIP\done-battleship-cd1.avi".

When needing double double literals, use: @"H:\MOVIES\Battel SHIP\done-battleship-cd1.avi";

Instead of: @"H:\MOVIESBattel SHIP\done-battleship-cd1.avi";

Because the first literals is for the path name and then the second literals are for the double quotation marks.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DR.
  • 573
  • 6
  • 20
  • this does not seem to work, I want to call ffmpeg.exe which sees these stupid microsoft weird conventions literally \"c:\\path\\file.txt\" ffmeg.exe says illegal arguments it needs some arguments double-quotes but WITHOUT the escape \ (back-slash). – Meryan Apr 01 '23 at 19:38
7

You can also include the double quotes into single quotes.

string str = '"' + "How to add doublequotes" + '"';
Yannick Turbang
  • 378
  • 4
  • 8
5

Using string interpolation with a working example:

var title = "Title between quotes";
var string1 = $@"<div>""{title}""</div>";  // Note the order of the $@
Console.WriteLine (string1);

Output

<div>"Title between quotes"</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
4

If you want to add double quotes to a string that contains dynamic values also. For the same in place of CodeId[i] and CodeName[i] you can put your dynamic values.

data = "Test ID=" + "\"" + CodeId[i] + "\"" + " Name=" + "\"" + CodeName[i] + "\"" + " Type=\"Test\";
Inder
  • 3,711
  • 9
  • 27
  • 42
3

You could use &quot; instead of ". It will be displayed correctly by the browser.

Jens
  • 25,229
  • 9
  • 75
  • 117
  • Nope doesn't work asp.net, c# vs2013 no matter how i code if you look and the page source " and ' are always displayed. I'm using `Attributes.Add(...' – djack109 May 18 '16 at 17:49
3

You can use $.

Interpolated Strings: Used to construct strings. An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations. This feature is available in C# 6 and later versions.

string commentor = $"<span class=\"fa fa-plus\" aria-hidden=\"true\"> {variable}</span>";
x19
  • 8,277
  • 15
  • 68
  • 126
1

In C# you can use:

 string1 = @"Your ""Text"" Here";
 string2 = "Your \"Text\" Here";
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Shivam Mishra
  • 319
  • 3
  • 15
1

Use either

&dquo;
<div>&dquo;"+ title +@"&dquo;</div>

or escape the double quote:

\"
<div>\""+ title +@"\"</div>
graham
  • 946
  • 5
  • 16
1
string doubleQuotedPath = string.Format(@"""{0}""",path);
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Sunil Kumar
  • 111
  • 1
  • 5
1

An indirect, but simple to understand alternative to add quotes at start and end of string -

char quote = '"';

string modifiedString = quote + "Original String" + quote;
Abhishek Poojary
  • 749
  • 9
  • 10
1

With C# 11.0 preview you can use Raw String Literals.

Raw string literals are a new format for string literals. Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences. A raw string literal starts with at least three double-quote (""") characters. It ends with the same number of double-quote characters. Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string.

string str = """
    "How to add double quotes"
    """;
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
0

Put a backslash (\) before the double quotes. That should work.

Belinda
  • 1,230
  • 2
  • 14
  • 25
0

In C#, if we use "" it means that it will indicate the following symbol is not a C# built-in symbol that will used by the developer.

So in a string we need double quotes, meaning we can put "" symbol before double quotes:

string s = "\"Hi\""
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Malathi Mals
  • 73
  • 13
0

"<i class=\"fa fa-check-circle\"></i>" is used with ternary operator with Eval() data binding:

Text = '<%# Eval("bqtStatus").ToString()=="Verified" ? 
       Convert.ToString("<i class=\"fa fa-check-circle\"></i>") : 
       Convert.ToString("<i class=\"fa fa-info-circle\"></i>"
0

Now with C# 11

var string1 = """before "inside" after""";

var string2 = """ "How to add double quotes" """;
Maruf
  • 354
  • 3
  • 8
-1

If you want to add double quotes in HTML

echo "<p>Congratulations, &#8220;" . $variable . "&#8221;!</p>";

Output

Congratulations, "Mr John"!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yogesh Sarvaiya
  • 551
  • 6
  • 16