insert() StringBuffer Example

java.lang.StringBuffer class comes with many methods to manipulate string. One of such methods is insert() overloaded 12 times taking different parameters. insert() StringBuffer is used to insert some data in the buffer.
Observe the 12 overloaded methods of insert() StringBuffer:
  1. public synchronized java.lang.StringBuffer insert(int, java.lang.Object);
  2. public synchronized java.lang.StringBuffer insert(int, java.lang.String);
  3. public synchronized java.lang.StringBuffer insert(int, char[]);
  4. public java.lang.StringBuffer insert(int, java.lang.CharSequence);
  5. public java.lang.StringBuffer insert(int, boolean);
  6. public synchronized java.lang.StringBuffer insert(int, char);
  7. public java.lang.StringBuffer insert(int, int);
  8. public java.lang.StringBuffer insert(int, long);
  9. public java.lang.StringBuffer insert(int, float);
  10. public java.lang.StringBuffer insert(int, double);
  11. public synchronized java.lang.StringBuffer insert(int, char[], int, int);
  12. public synchronized java.lang.StringBuffer insert(int, java.lang.CharSequence, int, int);

First parameter int is nothing but the index position (offset) in the buffer where to insert the second parameter. insert() method exists for all data types, arrays and objects.

Following example illustrates the usage of this overloaded method in all possible ways.

public class Demo
  public static void main(String args[])
  {				    // to insert int value at the beginning
   StringBuffer sb1 = new StringBuffer("ABCD");
   System.out.println("\nOriginal buffer sb1 before insert: " + sb1);
   int marks = 25;
   sb1.insert(0, marks);    
   System.out.println("Buffer sb1 after inserting int marks: " + sb1);

 				                // to insert string at the end
   String s1 = "HELLO";
   sb1.insert(sb1.length(), s1);    
   System.out.println("\nBuffer sb1 after inserting string: " + sb1);

 				                // to insert object (Date)
   sb1 = new StringBuffer("WXYZ");
   System.out.println("\nOriginal buffer sb1 before insert: " + sb1);
   sb1.insert(2, new java.util.Date());    	// observe, YZ goes to the end
   System.out.println("Buffer sb1 after inserting Date object: " + sb1);

 				                // to insert character array
   sb1 = new StringBuffer("PQRST");
   System.out.println("\nOriginal buffer sb1 before insert: " + sb1);
   char ch1[] = { '1', '2', '3' };
   sb1.insert(sb1.length(), "\n");	        // to insert a new line at the end
   sb1.insert(sb1.length(), ch1);	        // to insert array at the end
   System.out.println("Buffer sb1 after inserting new line and array: " + sb1);  
 }				               // comes in two lines, see output screen

insert() StringBuffer Example
Output screenshot on insert() StringBuffer Example

Like this, try for all possible data types and objects while observing the overloaded methods.

How to insert without using insert() method?

To insert you can do like this also without using insert() method.

  int marks = 25;  
  String result = String.format("Your marks are %d and you failed.", marks);

Leave a Comment

Your email address will not be published.