我有字符串:
string test = "\test.xml";
如何将其打印为:\ test.xml
答案 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";