473,386 Members | 1,693 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,386 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 6781
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
13
by: John Bowman | last post by:
Hi All, I've got a simple wrapper static test method on a class to expand the environment variables on a specified string: public static string ExpandEnvironmentStr(string Str) { return...
6
by: otto | last post by:
Hi, all: I would like to know one thing: Is it possible to access an environment variable (like PATH) from web.config file? Something like this: <appSettings> <add key="name"...
2
by: adam | last post by:
Hi ASP Expert, My goal is to retrieve my local machine's %USERNAME% environment variable from ASP page. When I enter http://RemoteServerName/testusername.asp?id=%USERNAME% directly into the IE...
2
by: =?Utf-8?B?SXbDoW4gU2Fpbno=?= | last post by:
I ran the aspnet_regsql.exe tool with the -sqlexportonly option to create the script. I searched and replace the string by and had the dba's run the script. When I do anything like...
0
by: Lloyd Sheen | last post by:
I have a couple of questions about URLs. I have an app which will allow me to access my music collection if I can access the internet. It shows a list of Artists / Genres / Search etc which...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.