Java - String Buffer replace() Method


Advertisements

Description:

This method replaces the characters in a substring of this StringBuffer with characters in the specified String.

The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start.

Syntax:

Here is the syntax of 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 SringBuffer 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 produces the following result:

abcZARAijk

java_string_buffer.htm

Advertisements