insert() StringBuffer Example

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.


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.

Leave a Reply

Your email address will not be published. Required fields are marked *