473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 39561
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Craig Keightley | last post by:
Can these lines of sql statements be consolidated into one sql statement (possibly using reg exps??) BEGIN CODE ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Update...
12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
6
by: Danny | last post by:
I need an asp command to strip out from a string all extra punctuation such as apostrophe, comma, period, spaces dashes, etc etc and just leave the letters. Can anybody give me some ideas? ...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Goran Djuranovic | last post by:
Hi all, I ran into a problem where my XMLTextReader fails on .Read() when I have "<" character in one of the attribute's values. What I am trying to do is replace illegal characters ("<", "&" ,...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
6
by: JackpipE | last post by:
Here is my replace query and I need to run this on every column in my table. Right now I manually enter the column name (_LANGUAGES_SPOKEN) but this is time consuming and would like to automate...
5
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
15
by: =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post by:
I have a text file that contains about 8 to 10 text sequences that I need to replace. I want to search and replace all 8 to 10 text sequence anytime I run this script Here is what I have so...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.