字符串和使用\

Cra*_*aig 0 c# string

我有stringBuilder和string类,存储路径:

StringBuilder name = new StringBuilder();
name.Append(@"NETWORK\MyComputer");
String serverName = name.ToString();  //this converts the \ to a \\
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多东西,但它总是导致字符串有\

使用serverName.Replace("\\", @"\");不起作用,它留下它作为\

servername.Replace("\\", "\"");"在字符串中 添加一个仍然不正确的字符串.

请协助.

slu*_*ter 7

如果你担心单个反斜杠被显示为双反斜杠,那么就不要 - 这就是它在调试器中显示给你的方式.

反斜杠是一个特殊的角色,通过加倍来关闭"特殊性".或者,@符号可以在源代码中以字符串为前缀,避免必须使用它.