Replace a character at a specific index in a string?

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

    String is an immutable class in java. Any method which seems to modify it always returns a new string object with modification.

    If you want to manipulate a string, consider StringBuilder or StringBuffer in case you require thread safety.

提交回复
热议问题