How to change 1 char in the string?

后端 未结 7 1944
天涯浪人
天涯浪人 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 17:52

    While it does not answer the OP's question precisely, depending on what you're doing it might be a good solution. Below is going to solve my problem.

    Let's say that you have to do a lot of individual manipulation of various characters in a string. Instead of using a string the whole time use a char[] array while you're doing the manipulation. Because you can do this:

     char[] array = "valta is the best place in the World".ToCharArray();
    

    Then manipulate to your hearts content as much as you need...

     array[0] = "M";
    

    Then convert it to a string once you're done and need to use it as a string:

    string str = new string(array);
    

提交回复
热议问题