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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With