SHuoDo
SHuoDo

Reputation: 97

how to use " as character in C#

I need to use " and ' as a character in C#, however they are special characters. So when I put them as character in the string, it will give an error. The issue is that it has many " and ' so I need to find a way to allow me to use these special characters. How can I do that

Upvotes: 0

Views: 1113

Answers (5)

Plasmarob
Plasmarob

Reputation: 1391

Put a backslash before special chars you want to use to escape them.

Here's a list:

http://msdn.microsoft.com/en-us/library/h21280bw.aspx

Upvotes: 0

Tilak
Tilak

Reputation: 30708

You can use escape character \

Example

var testSTring = "\"test\""

Upvotes: 2

Hogan
Hogan

Reputation: 70523

Simple prefix your string with @ for " use "" This makes newlines easy as pie:

string example = @"A string that has double quote "" and single quote ' also a new line
     ";

Upvotes: 3

Habib
Habib

Reputation: 223287

Having a single quote ' in string doesn't trouble C#, its the double quotes ", you have to escape them with backslash like:

 string str = "some\"stri'''''ng";

You can also use verbatim string @ in the start and then you have to escape double quotes with another double quote like:

string str = @"some""stri'ng";

Upvotes: 1

alecbz
alecbz

Reputation: 6488

Use escape sequences: "This is a double-quote: \", and this is a single quote: \'"

Although note that since the string is delimited by double quotes, the \' escape isn't necessary: "This is a double-quote: \", and this is a single quote: '"

Upvotes: 7

Related Questions