Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put quotes in a string?

Tags:

string

c#

quotes

I need to write a string literal to a text file, but the C# compiler finds errors when I use quote characters in it.

My current code:

writeText.WriteLine("<?xml version="1.0" encoding="utf-8"?>");

I need the output for the text file to be:

<?xml version="1.0" encoding="utf-8"?>

How can I put quote characters in strings in C#?

like image 663
riad Avatar asked May 26 '10 08:05

riad


People also ask

How do you write a double quote in a string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.


2 Answers

You need to escape the quotation marks to put them in a string. There is two ways of doing this. Using backslashes in a regular string:

writeText.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

Using double quoation marks in a @-delimited string:

writeText.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>");
like image 82
Guffa Avatar answered Nov 06 '22 03:11

Guffa


Try

writeText.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

Have a look at "What character escape sequences are available?" of the C# FAQ

like image 22
Binary Worrier Avatar answered Nov 06 '22 02:11

Binary Worrier