לא הכי יעיל אבל עושה את העבודה
במקום String תשתמש בStringBuffer יש לו מתודה
StringBuffer replace(int start, int end, String str) Replaces the characters in a substring of this StringBuffer with characters in the specified String.
וגם את המתודה:
String substring(int start) Returns a new String that contains a subsequence of characters currently contained in this StringBuffer.The substring begins at the specified index and extends to the end of the StringBuffer.
ואידך זיל גמור