Savage F. Morgan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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);

or you can use a StringBuilder:

StringBuilder myName = new StringBuilder("domanokz");
myName.setCharAt(4, 'x');

System.out.println(myName);

http://stackoverflow.com/questions/6952363/java-replace-a-character-at-a-specific-index-in-a-string

posted on 2014-10-29 11:32  罗斯摩根  阅读(396)  评论(0编辑  收藏  举报