Try below code. With help of StringBuffer you do replacement. StringBuffer replace java docs
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("123456789");
int start = 3;
int end = 6;
buf.replace(start, end, "foobar");
System.out.println(buf);
}
}
After running above program below will be out put.
123foobar789
Demo