469,643 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

How to replace the "\\" char with "\"

JPS
I need to build a string with just one backslash included "\", but I
keep getting an error message unless I use "\\". The double
backslashes will not work with what I am doing.

May 23 '07 #1
2 10445
JPS,

Did you look at the output? If you use "\\", you will find in the final
output that there is only one "\". The slash is an escape character for
strings in C# (unless you use the literal specifier @ before the string),
and because of that, if you want a single backslash, you need to have two in
the string.

That being said, these two statements are equivalent in C#:

str = "\\\\somemachine\\someshare\\somefile.txt";
str = @"\\somemachine\someshare\somefile.txt";

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JPS" <jp*****@yahoo.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
>I need to build a string with just one backslash included "\", but I
keep getting an error message unless I use "\\". The double
backslashes will not work with what I am doing.

May 23 '07 #2
If you're working with the string, you have to use the "\" to tell the
compiler that you want to use the literal "\" (or whatever character)
follows. The "\" is used for commands, so if you put just one "\" then it
will think it's a command instead of a "\"

"JPS" <jp*****@yahoo.comwrote in message
news:11********************@p77g2000hsh.googlegrou ps.com...
>I need to build a string with just one backslash included "\", but I
keep getting an error message unless I use "\\". The double
backslashes will not work with what I am doing.
May 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Darin Johnson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.