This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
String objects are immutable in Java, so that's why the String class doesn't provide any methods for changing its contents. As already stated, you can either use string concatenation or StringBuilder/StringBuffer whenever you need an editable string. (Note that most compilers will automatically use StringBuffer or StringBuilder for expressions involving multiple string concatenations.)
Here are a pair of StringBuilder-based code snippets to do the same thing as the two programs which Fudong posted:
[ November 13, 2007: Message edited by: Kelvin Lim ]
The code example by Jia Fudong : is a loose workaround the insert functionality. It will have to be modified in circumstances like when multiple instances of same char occur in which case indexOf() to look for in subString() wont work that good. I will suggest StringBuilder class method .