Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a string with special characters in C#

Tags:

string

c#

.net

How can I create a string which contains the following:

<Object type="System.Windows.Forms.Form
like image 275
Peter17 Avatar asked Nov 28 '22 10:11

Peter17


2 Answers

Use an escape character for the quote:

string temp = "<Object type=\"System.Windows.Forms.Form"

See the msdn article for more examples: http://msdn.microsoft.com/en-us/library/h21280bw.aspx

Edit

Correct link for C#: C# programming guide

like image 171
scottrudy Avatar answered Dec 11 '22 12:12

scottrudy


You have two choices, depending on the remainder of the text you want to place into the string:

use the escape character \ within the double-quoted string for any double-quote marks, as the other answers have suggested.

string s = "<Object type=\"System.Windows.Forms.Form";

use the string-@ form, which avoids processing the \ (such as in path names like C:\Temp\Myfile.txt), and then double the double-quote:

string s = @"<Object type=""System.Windows.Forms.Form";

See also: http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx

like image 39
Andy Finkenstadt Avatar answered Dec 11 '22 13:12

Andy Finkenstadt