To replace substring in a StringBuffer object, use replace()
.
Its signature is shown here:
StringBuffer replace(int startIndex, int endIndex, String str)
The following program demonstrates replace()
:
// Demonstrate replace() public class Main { public static void main(String args[]) { StringBuffer sb = new StringBuffer("This is a test."); sb.replace(5, 7, "was"); System.out.println("After replace: " + sb); }//ww w .j av a 2 s. c o m }