Java StringBuffer replace() Method



The Java StringBuffer replace() method is used to replace the characters in a substring of a StringBuffer object 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 sequence, if no such character exists.

The replace() method accepts three parameters as an integer and string that holds the values of the start, end, and str. It throws an exception if the start index value is negative or greater than the string length.

Syntax

Following is the syntax of the Java StringBuffer replace() method −

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

Parameters

  • start − This is the beginning index, inclusive.

  • end − This is the ending index, exclusive.

  • str − This is the String that will replace previous contents.

Return Value

This method returns this object.

Example

If the given start index value is positive, and less than the string length, the replace() method replaces the character in the specified string.

In the following program, we are instantiating the StringBuffer class with the value “Java Programming”. Then, using the replace() method, we are trying to replace the character of the sub-string “Language” of this sequence at the startIndex 5, and endIndex 16.

public class Replace {
   public static void main(String[] args) {
      //instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("Java Programming");
      System.out.println("The given String is: " + sb);
      //initialize the startIndex, endIndex, and string value
      int startIndex = 5;
      int endIndex = 16;
      String str = "Language";
      System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
      System.out.println("The given sub-string is: " + str);
      //using the replace() method
      System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
   }
}

Output

On executing the above program, it will produce the following result −

The given String is: Java Programming
The given startIndex and endIndex values are: 5 and 16
The given sub-string is: Language
After replace the string: Java Language

Example

If the given startIndex value is negative, the replace() method throws IndexOutOfBoundException.

In the following example, we are creating an object of the StringBuffer class with the value “TutorialsPoint”. Using the replace() method, we are trying to replace the characters of the sub-string “Point” of this sequence at the startIndex -1 and endIndex 10.

public class Replace {
   public static void main(String[] args) {
      try {
         //create an object StringBuffer class
         StringBuffer sb = new StringBuffer("TutorialsPoint");
         System.out.println("The given String is: " + sb);
         //initialize the startIndex, endIndex, and string value
         int startIndex = -1;
         int endIndex = 10;
         String str = "India";
         System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         System.out.println("The given sub-string is: " + str);
         //using the replace() method
         System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given String is: TutorialsPoint
The given startIndex and endIndex values are: -1 and 10
The given sub-string is: India
java.lang.StringIndexOutOfBoundsException: Range [-1, 10) out of bounds for length 14
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:987)
   at java.base/java.lang.StringBuffer.replace(StringBuffer.java:497)
   at Replace.main(Replace.java:16)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 10) out of bounds for length 14

Example

If the given start index value is greater than the string length, this method throws the IndexOutOfBoundException.

In this example, we are instantiating the StringBuffer class with the value “hello”. Using the replace() method, we are trying to replace the characters of the sub-string “llo” of this sequence at the startIndex 10 and endIndex 5.

public class Replace {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("hello");
         System.out.println("The given String is: " + sb);
         //initialize the startIndex, endIndex, and string value
         int startIndex = 10;
         int endIndex = 5;
         String str = "world";
         System.out.println("The given startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         System.out.println("The given sub-string is: " + str);
         //using the replace() method
         System.out.println("After replace the string: " + sb.replace(startIndex, endIndex, str));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Above program produces the following output −

The given String is: hello
The given startIndex and endIndex values are: 10 and 5
The given sub-string is: world
java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 5
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:987)
   at java.base/java.lang.StringBuffer.replace(StringBuffer.java:497)
   at Replace.main(Replace.java:16)
Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 5
java_lang_stringbuffer.htm
Advertisements