Is there an easy way to change a char in a string in C#?

后端 未结 8 1880
忘掉有多难
忘掉有多难 2020-12-10 16:02

I want to do this:

string s = \"abc\";
s[1] = \'x\';

and s will become \"axc\". However, it seems that string[i] only has a getter and has

8条回答
  •  萌比男神i
    2020-12-10 16:54

    (Your example is slightly wrong: s[2] = 'x' should change it to "abx".)

    No you can't, since strings are immutable, you have to create a new string:

    http://en.wikipedia.org/wiki/Immutable_object

    You should use a method that returns a new string with the desired modification.

    Hope that helps!

提交回复
热议问题