Replace a character at a specific index in a string?

后端 未结 8 935
我在风中等你
我在风中等你 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:36

    First thing I should have noticed is that charAt is a method and assigning value to it using equal sign won't do anything. If a string is immutable, charAt method, to make change to the string object must receive an argument containing the new character. Unfortunately, string is immutable. To modify the string, I needed to use StringBuilder as suggested by Mr. Petar Ivanov.

提交回复
热议问题