Learn StringBuffer Class in Java: Lecture 4

This article describes the important methods used in the Java StringBuffer class by providing suitable examples for each method.
Before reading further, here are the previous parts of this series.
In this lecture, we will learn an important method of the Java StringBuffer class, which is the insert() method.
 

Method: StringBuffer insert()

 
This method inserts the character or string within a given string at a specified position.
The following parameters are overloaded in this method.
  • StringBuffer insert(int offset, String str)
  • StringBuffer insert(int offset, Object obj)
  • StringBuffer insert(int offset, int i)
  • StringBuffer insert(int offset, long lng)
  • StringBuffer insert(int offset, float f)
  • StringBuffer insert(int offset, double d)
  • StringBuffer insert(int offset, boolean b)
  • StringBuffer insert(int offset, char c)
  • StringBuffer insert(int offset, char[] str)
  • StringBuffer insert(int index, char[] str, int offset, int len)
  • StringBuffer insert(int dstoffset, CharSequence s)
  • StringBuffer insert(int dstoffset, CharSequnce, int start, int end)
First of all, we will proceed with the top two methods.
  • StringBuffer insert(int offset, String str) inserts the string into the given character sequence. The characters of the string argument are inserted in order (thread-safe) into the given sequence at the specified or indicated offset and moving up to any character originally above that position and increasing the length of the sequence by the length of the string argument. It throws the exception StringIndexOutOfBoundsException() if the offset value is not valid.
     
  • StringBuffer insert(int offset, Object obj) inserts the string representation of the object argument into the given character sequence. The value of the offset argument should be greater than or equal to 0 (zero) or less than or equal to the length of the given sequence. It also throws the exception StringIndexOutOfBoundsException() if the offset value is not valid.
Let's proceed further to understand the example based on the preceding two methods.
Example

This example contains a description of the preceding insert methods.
  1. public class InsertStringObjectMethods   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("We coding");  
  6.         System.out.println("Output for insert(int offset, String str):");  
  7.         System.out.println();  
  8.         System.out.println("Buffer is: " + str1);  
  9.         str1.insert(2, " love"); //insert string at offset 2  
  10.         System.out.println("After inserting string at offset 2 is: " + str1.toString()); //prints StringBuffer after inserting  
  11.         str1 = new StringBuffer("1245689");  
  12.         System.out.println("Buffer is: " + str1);  
  13.         str1.insert(2, "3").insert(6, "7");  
  14.         System.out.println("After inserting strings at offset 2 & 5 is: " + str1.toString());  
  15.         System.out.println();  
  16.         System.out.println("Output for insert(int offset, Object obj):");  
  17.         System.out.println();  
  18.         StringBuffer str2 = new StringBuffer("Fourth");  
  19.         System.out.println("Buffer is: " + str2);  
  20.         Object obj1 = " lecture"//object to be inserted  
  21.         str2.insert(6, obj1); //insert string at offset 6  
  22.         System.out.println("After inserting string at offset 6 is: " + str2.toString()); //prints StringBuffer after inserting  
  23.         str2 = new StringBuffer("ABC123");  
  24.         System.out.println("Buffer is: " + str2);  
  25.         Object obj2 = "DEF";  
  26.         Object obj3 = "456";  
  27.         str2.insert(6, obj2).insert(9, obj3);  
  28.         System.out.println("After inserting strings at offset 6 & 8 is: " + str2.toString());  
  29.     }  
  30. }  
Output

Our next points of discussion are the following 5 methods.
  • Java StringBuffer insert(int offset, int i) inserts the string representation of an int argument into the given sequence. The offset argument value should be greater than or equal to 0 (zero) or less then or equal to the length of the argument.
     
  • Java StringBuffer insert(int offset, long lng) inserts the string representation of a long argument into the given sequence. The offset argument value should be greater than or equal to 0 (zero) or less than or equal to the length of the argument.
     
  • Java StringBuffer insert(int offset, float f) inserts the string representation of a float argument into the given sequence. The offset argument value should be greater than or equal to 0 (zero) or less than or equal to the length of the argument.
     
  • Java StringBuffer insert(int offset, double d) inserts the string representation of a long argument into the given sequence. The offset argument value should be greater than or equal to 0 (zero) or less than or equal to the length of the argument.
     
  • Java StringBuffer insert(int offset, Boolean b) inserts the string representation of a boolean argument into the given sequence. The offset argument value should be greater than or equal to 0 (zero) or less than or equal to the length of the argument.
All the preceding methods throws the exception StringIndexOutOfBoundsException() if the offset value is not valid.
Now, here's an example.
Example
 
This example contains a description of the 5 methods given above.
  1. public class InsertIntLongFloatDoublebooleanMethods   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         StringBuffer str1 = new StringBuffer("Got likes");  
  6.         System.out.println("Output for insert(int offset, int i):");  
  7.         System.out.println();  
  8.         System.out.println("Buffer is: " + str1);  
  9.         str1.insert(4, 137); //insert integer value at offset 3  
  10.         System.out.println("After inserting integer value at offset 3 is: " + str1.toString()); //prints StringBuffer after inserting  
  11.         str1 = new StringBuffer("@!&*$$*&!@");  
  12.         System.out.println("Buffer is: " + str1);  
  13.         str1.insert(1, 78).insert(7, 258).insert(14, 87);  
  14.         System.out.println("After inserting integer value at offset 1,7 & 14 is: " + str1.toString());  
  15.         System.out.println();  
  16.         System.out.println("Output for insert(int offset, long lng):");  
  17.         System.out.println();  
  18.         StringBuffer str2 = new StringBuffer("Call me at anytime");  
  19.         System.out.println("Buffer is: " + str2);  
  20.         str2.insert(11, 879129217); //insert long value at offset 11  
  21.         System.out.println("After inserting long value at offset 11 is: " + str2.toString()); //prints StringBuffer after inserting  
  22.         str2 = new StringBuffer(" OR ");  
  23.         System.out.println("Buffer is: " + str2);  
  24.         str2.insert(0, 987431378).insert(13, 979487639);  
  25.         System.out.println("After inserting long value at offset 0 & 13 is: " + str2.toString());  
  26.         System.out.println();  
  27.         System.out.println("Output for insert(int offset, float f):");  
  28.         System.out.println();  
  29.         StringBuffer str3 = new StringBuffer("Rate is per item");  
  30.         System.out.println("Buffer is: " + str3);  
  31.         str3.insert(8, 3.14); //insert float value at offset 8  
  32.         System.out.println("After inserting float value at offset 8 is: " + str3.toString()); //prints StringBuffer after inserting  
  33.         str3 = new StringBuffer("Min rate & max rate ");  
  34.         System.out.println("Buffer is: " + str3);  
  35.         str3.insert(9, 81.4).insert(25, 314.5);  
  36.         System.out.println("After inserting float value at offset 9 & 25 is: " + str3.toString());  
  37.         System.out.println();  
  38.         System.out.println("Output for insert(int offset, double d):");  
  39.         System.out.println();  
  40.         StringBuffer str4 = new StringBuffer("Pay amount");  
  41.         System.out.println("Buffer is: " + str4);  
  42.         str4.insert(4, 796594.8524); //insert double value at offset 4  
  43.         System.out.println("After inserting double value at offset 4 is: " + str4.toString()); //prints StringBuffer after inserting  
  44.         str4 = new StringBuffer("Total is + ");  
  45.         System.out.println("Buffer is: " + str4);  
  46.         str4.insert(9, 67633.87).insert(20, 213576.346);  
  47.         System.out.println("After inserting double value at offset 9 & 13 is: " + str4.toString());  
  48.         System.out.println();  
  49.         System.out.println("Output for insert(int offset, double d):");  
  50.         System.out.println();  
  51.         StringBuffer str5 = new StringBuffer("You are ");  
  52.         System.out.println("Buffer is: " + str5);  
  53.         str5.insert(8, true); //insert boolean value at offset 8  
  54.         System.out.println("After inserting boolean value at offset 8 is: " + str5.toString()); //prints StringBuffer after inserting  
  55.         str5 = new StringBuffer("You both are and else are ");  
  56.         System.out.println("Buffer is: " + str5);  
  57.         str5.insert(13, false).insert(32, true);  
  58.         System.out.println("After inserting boolean value at offset 13 & 32 is: " + str5.toString());  
  59.     }  
  60. }  
Output
 
 
In the next lecture, we will continue with a few more insert() methods along with some other methods. Until then, thank you, keep learning and sharing.


Similar Articles