For the following code, I can\'t get the string.Replace
to work:
someTestString.Replace(someID.ToString(), sessionID);
when I
someTestString = someTestString.Replace(someID.ToString(), sessionID);
that should work for you
You can achieve the desired effect by using
someTestString = someTestString.Replace(someID.ToString(), sessionID);
As womp said, strings are immutable, which means their values cannot be changed without changing the entire object.
Strings are immutable. The result of string.Replace
is a new string with the replaced value.
You can either store result in new variable:
var newString = someTestString.Replace(someID.ToString(), sessionID);
or just reassign to original variable if you just want observe "string updated" behavior:
someTestString = someTestString.Replace(someID.ToString(), sessionID);
Note that this applies to all other string
functions like Remove
, Insert
, trim and substring variants - all of them return new string as original string can't be modified.
strings are immutable, the replace will return a new string so you need something like
string newstring = someTestString.Replace(someID.ToString(), sessionID);