简体   繁体   中英

How to create a string with special characters in C#

How can I create a string which contains the following:

<Object type="System.Windows.Forms.Form

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

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

You can use the backslash character to escape strings, the example below should fit your needs:

Example:

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

MSDN Specification on String Literals / Escaping Literals:

MSDN: String Literals

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

Is that what you mean?

var str = "<Object type=\"System.Windows.Forms.Form";

Use backslash to escape.

String str = "<Object type=\"System.Windows.Forms.Forms";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM