logo

Java String Buffer Replace() Method


Show

Description

This technique replaces the characters in a substring of this StringBuffer with a font in the specified String. The substring starts at the specified begin and enlarges to the value at index end - 1 or to the end of the StringBuffer, if no such value exists. First, the characters in the substring are take away and then the particular String is inserted at the start.

Syntax

Here is the syntax for this method:

public StringBuffer replace(int start, int end, String str)

Parameters

Here is the detail of parameters:

  • start − The beginning index, inclusive.
  • end − The ending index, exclusive.
  • str − String that will replace previous contents.

Return Value

  • This method returns the modified StringBuffer object.

Example

public class Test {

   public static void main(String args[]) {
      StringBuffer sb = new StringBuffer("abcdefghijk");
      sb.replace(3, 8, "ZARA");
      System.out.println(sb); 
   }  
}

This will produce the following result:

Output

abcZARAijk

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.