簡體   English   中英

如何在C#中編寫以@開頭並包含“”的字符串

[英]How to write a string that starts with @ and contains “ ” in C#

當我更改以下C#字符串時

string xml="xmlns:qtpRep=\"http://www.mercury.com/qtp/ObjectRepository\""

進入

string xml=@"xmlns:qtpRep=\"http://www.mercury.com/qtp/ObjectRepository\""

我收到編譯器錯誤:;預期
如何在以@開頭的字符串中表達文字雙引號?

您可以將雙引號加倍而不是使用反斜杠:

string xml = @"xmlns:qtpRep=""http://www.mercury.com/qtp/ObjectRepository""";

(有關更多詳細信息,請參見MSDN 。)

盡管在這種情況下,仍然不清楚為什么仍要使用逐字字符串文字...對於XML屬性,使用單引號通常更簡單:

string xml = @"xmlns:qtpRep='http://www.mercury.com/qtp/ObjectRepository'";

我還必須說,如果您自己創建XML字符串,則可能是Doing It Wrong TM 改用XML API :)

當字符串逐字(帶有@ )時,兩個雙引號會轉換為單引號。

例如:

Console.WriteLine(@"this is ""enclosed in double quotes""");

...將會寫:

這是“用雙引號引起來”

將它們加倍:

 string xml = @"xmlns:qtpRep=\""http://www.mercury.com/qtp/ObjectRepository\""";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM