string.Replace (or other string modification) not working
Solution 1:
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.
Solution 2:
someTestString = someTestString.Replace(someID.ToString(), sessionID);
that should work for you
Solution 3:
strings are immutable, the replace will return a new string so you need something like
string newstring = someTestString.Replace(someID.ToString(), sessionID);
Solution 4:
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.