CODE
class StringBufferDemo6
{
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("Java Util Package"); // LINE A
System.out.println("Stringbuffer = " + buff);
// replace substring from index 5 to index 9
buff.replace(5, 9, "Lang"); // LINE B
// prints the stringbuffer after replacing
System.out.println("After replacing: " + buff);
}
}
Stringbuffer = Java Util Package
After replacing: Java Lang Package
In the above program at LINE A
we have created a StringBuffer
object with String "Java Util Package
" and printed it. At LINE B
we have replaced the substring starting at index 5
and ending at index 9
with "Lang
" and printed.
LINE B
in the above program with the code below. buff.replace(0, buff.length(), "Merit Campus");
LINE B
in the above program with the code below. buff.replace(4, 17, ".meritcampus.com");
4
and ending at 17
in the StringBuffer
with the ".meritcampus.com
"