Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - String Buffer replace() Method



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 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
java_strings.htm
Advertisements