如何在字符串的开头打印反斜杠(\)(c#)

时间:2014-02-25 12:03:00

标签: c# string backslash

我有字符串:

string test = "\test.xml";

如何将其打印为:\ test.xml

3 个答案:

答案 0 :(得分:12)

\ escape sequence character

Escape Sequence -- Represents

             \\ -- Backslash

如果要在字符串中使用它,则应使用另一个\字符将其转义。

string test = "\\test.xml";

或者您可以使用带有@字符的 verbatim string literal ;

string test = @"\test.xml";

答案 1 :(得分:4)

只需将转义序列添加为:

string test = "\\test.xml";

更好的选择是在前面添加'@'以避免字符串中的所有转义序列:

string test = @"\test.xml";

答案 2 :(得分:1)

string test = @"\test.xml";

string test = "\\test.xml";