2010-09-01 78 views
5

C#newbie here。我試圖設置一個C#變量,如下所示。如何使用C#中的行情創建一個字符串#

string profileArg = @"/profile ""eScore"" "; 

最終的結果是,我希望變量包含「eScore」

我該怎麼做之後的價值

/profile "eScore" 

應該有字符串中的空間?

賽斯

+5

我很困惑您的問題,'串profileArg = @「/ profile」「eScore」「」;'似乎正是你想要的。 – 2010-09-01 19:15:05

+0

也許你沒有看到尾部空格,因爲它是空白:) – digEmAll 2010-09-01 19:45:41

回答

12

你有eScore後空間,在你的字符串。

// a space after "eScore" 
string profileArg = @"/profile ""eScore"" "; 

// no space after "eScore" 
string profileArg = @"/profile ""eScore"""; 

// space in "eScore " 
string profileArg = @"/profile ""eScore """; 

// No space using escaping 
string profileArg = "/profile \"eScore\""; 
1
string profileArg = "/profile \"eScore\" "; 
0

試試這個:

string profileArg = "/profile \"eScore\" "; 

你想要把\"任何字面雙引號。

+0

nope。那麼不要使用at符號。 – jgauffin 2010-09-01 19:11:00

+0

對不起,我不打算離開@符號。 – drpcken 2010-09-01 19:14:17

+0

這不會編譯 – 2010-09-01 19:14:53

12

您似乎已經在正確地做到這一點。

2
string profileArg = "/profile \"eScore\" "; 
+1

反斜槓不用於在逐字字符串中轉義(@使其逐字)使用雙「」代替(如VB) – 2010-09-01 19:13:36

+0

剛剛發現這個錯誤 – lumberjack4 2010-09-01 19:14:21

0

添加到其他。 。 。第一個引號前面的@符號告訴C#不要將反斜槓\解釋爲轉義字符。這就是爲什麼給出的示例省略了@符號。然後你可以使用\"來加引號。

1

2個選擇:

  • 正常反斜槓轉義: 「這是\的測試」 行情\ 「」。
  • @ string double escaped:@「這是對」「引號」「的測試。」

這兩個應該包含相同的字符串:

This is a test of "Quotes". 
1

一種可能性是

string profileArg = "/profile \"eScore\" "; 

對我來說這看起來比更清晰的逐字直譯

0

在這裏你去

String test = " /profile \"eScore\"  "; 
相關問題