首页 > 编程语言 > 详细

Java - replace a character at a specific index in a string?

时间:2014-10-29 12:52:18      阅读:208      评论:0      收藏:0      [点我收藏+]

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

Java - replace a character at a specific index in a string?

原文:http://www.cnblogs.com/savagemorgan/p/4059044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!