-2

我想做类似的事情:

string s = "\\blabla";

当你写“\”时,这意味着只有一个'\'。如何编写字符串,以便实际上有 2 '\' 表示 "\" ?

4

5 回答 5

10

This works without a problem:

string s = "//blabla";

If you mean the backslash instead, you can use a verbatim string literal (using the @ symbol to avoid processing escape symbols):

string s = @"\\blabla";

Alternatively you can escape the escape character itself:

string s = "\\\\blabla";
于 2013-07-08T14:25:57.060 回答
7

'/' is not the escape char, so you can simply write "//"

The escape char is '\' and, to use it properly, you can refer to the MSDN instructions.

于 2013-07-08T14:25:45.640 回答
2

Try this:

string s = @"\\blabla";

The '@' symbol treats whatever follows it as a verbatim string literal (ie: you won't need to worry about escape characters within the string).

于 2013-07-08T14:26:42.363 回答
1

I think you mean \ not /.

You could escape the \ with another backslash "\\\\" or you could use a string literal @"\\"

于 2013-07-08T14:26:35.687 回答
0

您可以在字符串之前使用 @ 以避免转义特殊字符。

@-quoting 的优点是不处理转义序列,方便编写

http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx

于 2013-07-08T14:28:07.367 回答