更新时间:2022-12-28 12:51:51
Petar Ivanov 对在字符串问题中替换特定索引处的字符
字符串在Java中是不可变的。您不能更改它们。
String are immutable in Java. You can't change them.
您需要创建一个替换了字符的新字符串。
You need to create a new string with the character replaced.
String myName = "domanokz";
String newName = myName.substring(0,4)+'x'+myName.substring(5);
或者你可以使用StringBuilder:
Or you can use a StringBuilder:
StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');
System.out.println(myName);