Replace a character at a specific index in a string?

后端 未结 8 915
我在风中等你
我在风中等你 2020-11-22 03:22

I\'m trying to replace a character at a specific index in a string.

What I\'m doing is:

String myName = \"domanokz\";
myName.charAt(4) = \'x\';
         


        
8条回答
  •  名媛妹妹
    2020-11-22 03:46

    You can overwrite a string, as follows:

    String myName = "halftime";
    myName = myName.substring(0,4)+'x'+myName.substring(5);  
    

    Note that the string myName occurs on both lines, and on both sides of the second line.

    Therefore, even though strings may technically be immutable, in practice, you can treat them as editable by overwriting them.

提交回复
热议问题