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

后端 未结 8 1886
忘掉有多难
忘掉有多难 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条回答
  •  借酒劲吻你
    2020-12-10 16:47

    yes in c# string can not be altered.

    but we can try this

    string s = "abc";
    s = s.Replace('b', 'x');
    Console.WriteLine(s);
    

    answer will be "axc". as this will replace the old string with new string.

提交回复
热议问题