How can I replace the "\\" in a string with a double slash "\\\\"?
I tried String.Replace("\\","\\\\") but then intellisense stops working :(
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
\\r
- Carriage return (Windows newlines are \\r\\n
)
\\t
- Tab
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.
string.Replace(@"\\", @"\\\\")
would work just fine -- the @
tells the compiler to ignore the escaping of \\
.
Alternatively, \\\\
means one backslash -- so string.Replace("\\\\", "\\\\\\\\")
would work just fine too (although it's a bit unreadable).
使用'@'使反斜杠失去其特殊含义:
String.Replace(@"\", @"\\")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.