all groups > c# > february 2006 >
You're in the

c#

group:

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


String.Replace() in .net 1.1 does NOT work! Andrew
2/22/2006 7:01:27 PM
c#:
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.

Re: String.Replace() in .net 1.1 does NOT work! Pete Davis
2/22/2006 10:00:50 PM
Actually, String.Replace works just fine.

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

Pete


[quoted text, click to view]

Re: String.Replace() in .net 1.1 does NOT work! Bob Grommes
2/22/2006 10:06:23 PM
In addition, I believe that the first line needs to be either:

string pathVal = "2006\\";

//or:

string pathVal = @"2006\";

--Bob

[quoted text, click to view]
Re: String.Replace() in .net 1.1 does NOT work! Jon Skeet [C# MVP]
2/23/2006 12:00:00 AM
[quoted text, click to view]

It worked in that it did what it should have done (nothing) - it just
didn't do what you expected.

[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: String.Replace() in .net 1.1 does NOT work! tjb
2/23/2006 12:00:00 AM
[quoted text, click to view]

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\";
AddThis Social Bookmark Button