Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily display double quotes in strings in C#?

Tags:

string

c#

If you have a string with a numerous double quotes,

in PHP you can do this:

file.WriteLine('<controls:FormField Name="Strasse" LabelText="Strasse">');

in C# you have to do this:

file.WriteLine("<controls:FormField Name=\"Strasse\" LabelText=\"Strasse\">");

Is there a way in C# to do what you can do above in PHP, something like the @"c:\temp" which you can do so that you don't need double slashes?

Thanks Fredrik, that makes even quotes and curly brackets in a String.Format fairly readable:

    file.WriteLine(String.Format(@"<TextBox Text=""{{Binding {0}}}""
 Style=""{{DynamicResource FormularFieldTextBox}}""/>", fieldName));
like image 1000
Edward Tanguay Avatar asked Oct 20 '09 13:10

Edward Tanguay


People also ask

How do you display double quotes in a string?

'\' is the escape sequence that is used to insert a double quote. Below we can see that we are using this escape sequence inside our string, and the output shows the string with quotations.

How do you print double quotes in C?

\" - escape sequence Since printf uses ""(double quotes) to identify starting and ending point of a message, we need to use \" escape sequence to print the double quotes.


2 Answers

There are two ways to represent quotes in C# strings:

file.WriteLine("<controls:FormField Name=\"Strasse\" LabelText=\"Strasse\">");
file.WriteLine(@"<controls:FormField Name=""Strasse"" LabelText=""Strasse"">");
like image 85
Fredrik Mörk Avatar answered Sep 19 '22 06:09

Fredrik Mörk


"<controls:FormField Name='Strasse' LabelText='Strasse'>".Replace("'", "\"")

Which isn't great, but about the only option.

Or @"""" insted of \" you can use ""

like image 21
Yuriy Faktorovich Avatar answered Sep 17 '22 06:09

Yuriy Faktorovich