I have a string like:
string s = "abc";
And I want to change the second character to a 'd' so it will be "adc" I thought this would work:
s[1] = 'd';
However I get an error:
Property or indexer 'string.this[int] 'cannot be assigned to -- it is read only
How can I accomplish this easy task (without using reasonably complicated substring methods?
String
is imutable. You can use StringBuilder
to mutate a string:
var s = "abc";
var sb = new StringBuilder(s);
sb[1] = 'd';
s = sb.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With