How to change 1 char in the string?

后端 未结 7 1926
天涯浪人
天涯浪人 2021-01-03 17:43

I have this code:

string str = \"valta is the best place in the World\";

I need to replace the first symbol. When I try this:



        
7条回答
  •  情歌与酒
    2021-01-03 18:11

    I suggest you to use StringBuilder class for it and than parse it to string if you need.

    System.Text.StringBuilder strBuilder = new System.Text.StringBuilder("valta is the best place in the World");
    strBuilder[0] = 'M';
    string str=strBuilder.ToString();
    

    You can't change string's characters in this way, because in C# string isn't dynamic and is immutable and it's chars are readonly. For make sure in it try to use methods of string, for example, if you do str.ToLower() it makes new string and your previous string doesn't change.

提交回复
热议问题