Replace vs .Replace extension

arabgogs

Member
Joined
Nov 6, 2012
Messages
14
Location
Scotland
Programming Experience
5-10
Hi Guys,

I have a strange anomaly happening with the string replace functionality, that has had me toiling for an hour or so until I used the older assignment.


I had a string JBlah and was wanting to replace the Blah with "". I used myString.replace("Blah",""), which did absolutely nothing, however when I used the older myString = Replace("Blah","") we had success.

Can anyone advise why one would work over the other?
 
String.Replace is also a function, the result is the return value, input string is not changed.

It is not an extension method by the way, it is a regular method defined in String class.
 
Hi Guys,

I have a strange anomaly happening with the string replace functionality, that has had me toiling for an hour or so until I used the older assignment.


I had a string JBlah and was wanting to replace the Blah with "". I used myString.replace("Blah",""), which did absolutely nothing, however when I used the older myString = Replace("Blah","") we had success.

Can anyone advise why one would work over the other?
String.Replace is a function, so you need to capture the output like so:
myString = myString.replace("Blah",String.Empty)
 
Hi Guys,


Thank you for the replies and sorry for the late delay in response.

I was using both as assignment statements, it was just strange that the mystring = mystring.replace("blah","") was not working whilst the classic replace was. I was just wondering if there was any difference between the functionality of both. I was assuming the basic fundamentals of string replace would be identical.
 
Back
Top