469,271 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

replace \\ with \ in c#

mp
when I write this in c# :
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\", "\");

it doesnt like the last "\", and I want to replace \\ with \

what should I do?

Thanks
Nov 16 '05 #1
5 38651
mp,

The \ character is an escape character. You either have to double them
up, or use a string literal:

// Using string literals.
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace(@"\\", @"\");

// Using escape sequences.
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\\\", "\\");

Hope this helps.

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

"mp" <jo*****@hotmail.com> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...
when I write this in c# :
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\", "\");

it doesnt like the last "\", and I want to replace \\ with \

what should I do?

Thanks

Nov 16 '05 #2
try:
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace(@"\\", @"\");
"mp" <jo*****@hotmail.com> a écrit dans le message de news:
eG**************@TK2MSFTNGP10.phx.gbl... when I write this in c# :
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\", "\");

it doesnt like the last "\", and I want to replace \\ with \

what should I do?

Thanks

Nov 16 '05 #3
"mp" <jo*****@hotmail.com> wrote in news:eG**************@TK2MSFTNGP10.phx.gbl:
when I write this in c# :
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\", "\");

it doesnt like the last "\", and I want to replace \\ with \

what should I do?


mp,

In C# string literals, the \ character tells the C# compiler to treat
the following character in a special way. E.g. \n is interpreted
as a newline character, \t as a tab, etc.

http://msdn.microsoft.com/library/de...ec_2_4_4_5.asp

or

http://tinyurl.com/4zncw
To prevent this from happening, precede the string with the @ symbol.

strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace(@"\\", @"\");
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #4
hi mp!

to get one "\" in a string you need to double them, ie "\\"
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\\\", "\\");
or you can place a @ symbol in front of the string literal so the system
parses it like a good ol' VB string (no escape/specials characters) : strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace(@"\\", @"\");


--
Best Regards
Yanick
Nov 16 '05 #5
Try using verbatim string literals:

strFileToLoad.Substring(2).Replace(@"\\", @"\");

Regards,
Joakim

mp wrote:
when I write this in c# :
strFileToLoad = strFileToLoad.Substring(0, 2) +
strFileToLoad.Substring(2).Replace("\\", "\");

it doesnt like the last "\", and I want to replace \\ with \

what should I do?

Thanks

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Craig Keightley | last post: by
6 posts views Thread by JackpipE | last post: by
15 posts views Thread by =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.