Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Environment with string replace ....

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gabriel - 29 Sep 2007 15:57 GMT
Hello,

I do this :

s = Environment.CurrentDirectory;
s = s.Replace("\\", @"\");
Environment.CurrentDirectiry return a path like this C:\\....\\....\\.....
I'd like replace the \\ by \, but the code I use not work. Any idea ?

Best Regards,
Alex Meleta - 29 Sep 2007 16:26 GMT
Hi Gabriel,

You forgot @ at first string.
s.Replace(@"\\", @"\");

Regards, Alex
[TechBlog] http://devkids.blogspot.com

> s.Replace("\\", @"\");
Gabriel - 29 Sep 2007 16:44 GMT
> You forgot @ at first string.
> s.Replace(@"\\", @"\");

Hello Alex,

It's strange that. In the debugger I see \\ but when I display the path in
the application caption I see \

Thanks,
Smithers - 29 Sep 2007 19:10 GMT
The back slash is a special character to the C# compiler. So, when you want
to use it in a string, you must place an escape character in front of it.
The escape character is also a back slash. (alternatively, you can place the
'@' character in front of a string to indicate to the compiler that the
following is a literal string, with no special characters in it.

Consequently the compiler treats or sees
     "\\"
and
     @"\"
as the exact same string.

So your original replace operation , s = s.Replace("\\", @"\"); ,  replaces
a single back slash with a single back slash. This is a nonsensical
operation.

If you want to replace two back slashes with a single back slash, either of
these should work:
     s = s.Replace("\\\\", "\\");
or
     s = s.Replace(@"\\", @"\");

Note that this "escape character" stuff is all for the compiler. So the
comiler will, for example, translate "\\" to be "\" in the output assembly.
This is why you see "\\" in Visual Studio, when your application shows "\"

-HTH

-S

>> You forgot @ at first string.
>> s.Replace(@"\\", @"\");
[quoted text clipped - 5 lines]
>
> Thanks,
Jon Skeet [C# MVP] - 29 Sep 2007 19:20 GMT
> > You forgot @ at first string.
> > s.Replace(@"\\", @"\");
>
> It's strange that. In the debugger I see \\ but when I display the path in
> the application caption I see \

What you're seeing in the debugger is the string literal you'd have to
type in to get the actual string. It catches lots of people out. The
*real* string only has a single backslash.

Jon
james - 29 Sep 2007 16:45 GMT
Gabriel,

Do Console.WriteLine(s) and it prints correctly.  Internally \\ is the
code for backslash... the first backslash gets special handling.  Its
used for \n is newline, \t is tab, and \\ is backslash.

-James
Cor Ligthert[MVP] - 29 Sep 2007 17:35 GMT
Gabriel,

Probably you achieve your goal easier with the path class.
http://msdn2.microsoft.com/en-us/library/system.io.path(VS.71).aspx

Cor

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.