471,319 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

String.Replace() in .net 1.1 does NOT work!

Hi, guys,

The following source code did not work:

string pathVal = "2006\";
pathVal.Replace("\\", "/");

I still had the value "2006\", not "2006/", the one I expected.

I then tried the second line as:

pathVal.Replace('\', '/');

still no luck.

Any ideas? Thanks.
Feb 23 '06 #1
4 3905
tjb
Andrew <An****@discussions.microsoft.com> wrote:
The following source code did not work:

string pathVal = "2006\";
pathVal.Replace("\\", "/");


String.Replace returns (a reference to) a _new_ instance containing the new
value. It does not change the value of the current instance, because it
cannot -- strings are immutable (a string's value cannot be changed). Here
is how to achieve what you're attempting:

string pathVal = "2006\";
pathVal = pathVal.Replace("\\", "/");
Feb 23 '06 #2
Actually, String.Replace works just fine.

Try:
pathVal = pathVal.Replace("\\", "/")

Pete
"Andrew" <An****@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.com...
Hi, guys,

The following source code did not work:

string pathVal = "2006\";
pathVal.Replace("\\", "/");

I still had the value "2006\", not "2006/", the one I expected.

I then tried the second line as:

pathVal.Replace('\', '/');

still no luck.

Any ideas? Thanks.

Feb 23 '06 #3
In addition, I believe that the first line needs to be either:

string pathVal = "2006\\";

//or:

string pathVal = @"2006\";

--Bob

tjb wrote:
Andrew <An****@discussions.microsoft.com> wrote:

The following source code did not work:

string pathVal = "2006\";
pathVal.Replace("\\", "/");

String.Replace returns (a reference to) a _new_ instance containing the new
value. It does not change the value of the current instance, because it
cannot -- strings are immutable (a string's value cannot be changed). Here
is how to achieve what you're attempting:

string pathVal = "2006\";
pathVal = pathVal.Replace("\\", "/");

Feb 23 '06 #4
Andrew <An****@discussions.microsoft.com> wrote:
The following source code did not work:

string pathVal = "2006\";
pathVal.Replace("\\", "/");
It worked in that it did what it should have done (nothing) - it just
didn't do what you expected.
I still had the value "2006\", not "2006/", the one I expected.

I then tried the second line as:

pathVal.Replace('\', '/');

still no luck.

Any ideas? Thanks.


Strings are immutable. Calling Replace doesn't change the current
string, it just returns a new one.

Read http://www.pobox.com/~skeet/csharp/strings.html for more about
strings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Wim Roffal | last post: by
18 posts views Thread by Steve Litvack | last post: by
32 posts views Thread by tshad | last post: by
2 posts views Thread by Joebloggs | last post: by
8 posts views Thread by ronrsr | last post: by
reply views Thread by rosydwin | 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.