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 insert() Method



Description

This method inserts the data into a substring of this StringBuffer. We should specify the offset value (integer type) of the buffer, at which we need to insert the data. Using this method, data of various types like integer, character, string etc. can be inserted.

If start is equal to end, no changes are made.

Syntax

Here is a separate method for each primitive data type −

public StringBuffer insert(int offset, boolean b)
public StringBuffer insert(int offset, char c)
public insert(int offset, char[] str)
public StringBuffer insert(int index, char[] str, int offset, int len)
public StringBuffer insert(int offset, float f)  
public StringBuffer insert(int offset, int i)
public StringBuffer insert(int offset, long l) 
public StringBuffer insert(int offset, Object obj) 
public StringBuffer insert(int offset, String str)

Parameters

Here is the detail of parameters −

  • Parameter depends on what you are trying to insert.

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.insert(3, "123");
      System.out.println(sb); 
   }  
}

This will produce the following result −

Output

abc123defghijk
java_strings.htm
Advertisements