[英]String.Replace with \ in it?
How can I replace the "\\" in a string with a double slash "\\\\"? 如何用双斜杠“\\\\”替换字符串中的“\\”?
I tried String.Replace("\\","\\\\") but then intellisense stops working :( 我尝试了String.Replace(“\\”,“\\\\”)但是然后intellisense停止工作:(
Thanks! 谢谢!
Try: 尝试:
String.Replace("\\","\\\\")
This is because a character can follow \\, which makes a special character. 这是因为一个角色可以跟随\\,这就是一个特殊的角色。 \\" means put a literal double quote in the string, rather than close it.
\\“意味着在字符串中放入文字双引号,而不是关闭它。
Here are some common ones: 以下是一些常见的:
\\n
- Line feed \\n
- 换行
\\r
- Carriage return (Windows newlines are \\r\\n
) \\r
- 回车(Windows换行符为\\r\\n
)
\\t
- Tab \\t
- 标签
The other answers, which say to use @"\\"
are right and easier to understand, so should probably be used instead. 其他答案,即使用
@"\\"
是正确的,更容易理解,所以应该使用它。
\\
is a reserved character in a string, it's an "escape". \\
是字符串中的保留字符,它是一个“转义”。 So, for instance, \\n
means a linefeed constant. 因此,例如,
\\n
表示换行常量。
string.Replace(@"\\", @"\\\\")
would work just fine -- the @
tells the compiler to ignore the escaping of \\
. string.Replace(@"\\", @"\\\\")
可以正常工作 - @
告诉编译器忽略\\
的转义。
Alternatively, \\\\
means one backslash -- so string.Replace("\\\\", "\\\\\\\\")
would work just fine too (although it's a bit unreadable). 或者,
\\\\
表示一个反斜杠 - 所以string.Replace("\\\\", "\\\\\\\\")
也可以正常工作(虽然它有点不可读)。
使用'@'使反斜杠失去其特殊含义:
String.Replace(@"\", @"\\")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.