0

如何打印单个反斜杠

我想打印 c:\test\

如果我使用

      string IN = "C:\test\\";

它的出现是

      "C:\test\\"

如果我使用

      string IN = @"C:\test\";

它的出现是

      "C:\\test\\"

这是怎么回事 ?

4

3 回答 3

8

I assume by "coming out" you mean how it looks in the debugger or some other visualization. The debugger shows all control characters, so it uses \\ to indicate one slash. Otherwise you wouldn't know if \t meant a slash followed by a t or a Tab character.

Either of these should give you the appropriate string:

string IN = @"C:\test\";

or

string IN = "C:\\test\\";
于 2013-08-06T14:54:07.983 回答
2

Where are you printing to? It will always show \\ in the debug and watch windows but if you do a Console.WriteLine("\\") you will get only a single backslash.

You could also try clicking the small magnifying glass icon next to watched values and on debug tooltips. This will show you the string as it will appear when printed - ie. without all the escape sequences.

于 2013-08-06T14:53:58.807 回答
0

你在这里完全没问题。没什么好担心的。反斜杠被转义,这就是它们被加倍的原因。

如果你把这个

string IN = "C:\\test\";
Console.WriteLine(IN);

输出将是

C:\test\
于 2013-08-06T15:00:21.740 回答