100

I have a string "I want to learn "c#"". How can I include the quotes before and after c#?

one noa
  • 345
  • 1
  • 3
  • 10
learning
  • 11,415
  • 35
  • 87
  • 154
  • 2
    possible duplicate of [How can I put quotes in a string?](http://stackoverflow.com/questions/2911073/how-can-i-put-quotes-in-a-string) – Oliver Aug 11 '10 at 12:18

8 Answers8

201

Escape them with backslashes.

"I want to learn \"C#\""
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • The reference manual is helpful: http://msdn.microsoft.com/en-us/library/ms228362.aspx – S.Lott Aug 11 '10 at 12:17
  • 2
    you cannot use multiple """ when formatting a string String.Format("", params) will not work with multiple quotes. please use this answer that is marked correctly and get used to the habit of doing it. – New Bee Sep 04 '14 at 07:09
  • @ANeves fair to say that @newbee is wrong and that there's a Working counter-example: `string ok = string.Format(@"""{0}"" = {1}", "yes", true);` but don't link to stupid irrelevant pictures that waste peoples' time. This is a technical site – barlop Aug 08 '16 at 22:23
107

As well as escaping quotes with backslashes, also see SO question 2911073 which explains how you could alternatively use double-quoting in a @-prefixed string:

string msg = @"I want to learn ""c#""";

which results in:

I want to learn "c#"

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
NeilDurant
  • 2,082
  • 2
  • 14
  • 14
  • I needed this in my replace logic. string nullHideDecimal = @""; and then dataContractXML = dataContractXML.Replace(nullHideDecimal, "0"); – Ziggler Jan 27 '16 at 19:56
30

I use:

var value = "'Field1','Field2','Field3'".Replace("'", "\""); 

as opposed to the equivalent

var value = "\"Field1\",\"Field2\",\"Field3\"";

Because the former has far less noise than the latter, making it easier to see typo's etc.

I use it a lot in unit tests.

Mdev
  • 2,440
  • 2
  • 17
  • 25
James Cochrane
  • 309
  • 3
  • 3
17
string str = @"""Hi, "" I am programmer";

OUTPUT - "Hi, " I am programmer

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
somesh
  • 3,508
  • 4
  • 16
  • 10
6

Since .NET 7 you can use raw string literals, which allows to declare strings without escaping symbols:

string text = """
I want to learn "C#"
""";
Console.WriteLine(text); // Prints string 'I want to learn "C#"'

If string does not start or end with double quote you can even make it single line:

string text = """I want to learn "C#"!""";
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
4

Use escape characters for example this code:

var message = "I want to learn \"c#\"";
Console.WriteLine(message);

will output:

I want to learn "c#"

Łukasz W.
  • 9,538
  • 5
  • 38
  • 63
1

You can also declare a constant and use it each time. neat and avoids confusion:

const string myStrQuote = "\"";
Chagbert
  • 722
  • 7
  • 16
-4

The Code:

string myString = "Hello " + ((char)34) + " World." + ((char)34);

Output will be:

Hello "World."

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
Ariel Terrani
  • 618
  • 6
  • 6