By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

replace \\ with \ in c#

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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.