2014-02-25 31 views
-4

我有串打印反斜線(\):如何在一個字符串(C#)開始

string test = "\test.xml"; 

我怎樣才能把它打印出來的:\的test.xml

+0

string test =「\\ test.xml」; –

+1

只是一個簡短的評論:你完全可以編譯這個代碼的唯一原因是「\ t」是一個製表符的轉義序列。 – Dirk

+1

誰降低了這個?你不必谷歌你知道的一切!在SO上提出簡單的問題有什麼不對? – Alex

回答

12

\escape sequence character

Escape Sequence -- Represents 

      \\ -- Backslash 

如果你想在你的字符串中使用它,你應該與其他\字符轉義。

string test = "\\test.xml"; 

或者您可以使用verbatim string literal@字符等;

string test = @"\test.xml"; 
+1

+1我個人比較喜歡@「...」,因爲它使得文本更具可讀性,並且擺脫了所有醜陋的逃脫。 – Alex

+0

@Alex:它還允許保留多行字符串,因爲這對於SQL查詢尤其方便。 –

+0

是的,其實''\ t''是_one_字符,[TAB](http://en.wikipedia.org/wiki/Tab_key#Tab_characters)。每個「e」,「s」,「t」,「'」,「x」,「m」,「l」都是一個字符,所以來自問題的字符串長度爲「8」,而不是「9」。 –

4

只需添加轉義序列爲:

string test = "\\test.xml"; 

更好的選擇是前面加上 '@',以避免字符串中的所有轉義序列爲:

string test = @"\test.xml"; 
1
string test = @"\test.xml"; 

string test = "\\test.xml"; 
+0

你的答案中的''''''也可以通過Stack Overflow軟件逃脫......(我也需要編輯這個評論。) –

+2

不知何故,不是嗎? –

相關問題