471,071 Members | 1,205 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Environment with string replace ....

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,
Sep 29 '07 #1
6 6610
Hi Gabriel,

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

Regards, Alex
[TechBlog] http://devkids.blogspot.com
s.Replace("\\", @"\");

Sep 29 '07 #2
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

Sep 29 '07 #3
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,

Sep 29 '07 #4
Gabriel,

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

Cor
Sep 29 '07 #5
On Sep 29, 4:44 pm, "Gabriel" <nos...@nospam.comwrote:
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

Sep 29 '07 #6
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

"Gabriel" <no****@nospam.comwrote in message
news:Or****************@TK2MSFTNGP06.phx.gbl...
>
>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,

Sep 29 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by tshad | last post: by
13 posts views Thread by John Bowman | last post: by
2 posts views Thread by =?Utf-8?B?SXbDoW4gU2Fpbno=?= | last post: by
reply views Thread by Lloyd Sheen | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by leo001 | last post: by

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.