string.Replace (or other string modification) not working

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.

Leave a Comment