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.
- public class InsertStringObjectMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("We coding");
- System.out.println("Output for insert(int offset, String str):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.insert(2, " love");
- System.out.println("After inserting string at offset 2 is: " + str1.toString());
- str1 = new StringBuffer("1245689");
- System.out.println("Buffer is: " + str1);
- str1.insert(2, "3").insert(6, "7");
- System.out.println("After inserting strings at offset 2 & 5 is: " + str1.toString());
- System.out.println();
- System.out.println("Output for insert(int offset, Object obj):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Fourth");
- System.out.println("Buffer is: " + str2);
- Object obj1 = " lecture";
- str2.insert(6, obj1);
- System.out.println("After inserting string at offset 6 is: " + str2.toString());
- str2 = new StringBuffer("ABC123");
- System.out.println("Buffer is: " + str2);
- Object obj2 = "DEF";
- Object obj3 = "456";
- str2.insert(6, obj2).insert(9, obj3);
- System.out.println("After inserting strings at offset 6 & 8 is: " + str2.toString());
- }
- }
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.
- public class InsertIntLongFloatDoublebooleanMethods
- {
- public static void main(String args[])
- {
- StringBuffer str1 = new StringBuffer("Got likes");
- System.out.println("Output for insert(int offset, int i):");
- System.out.println();
- System.out.println("Buffer is: " + str1);
- str1.insert(4, 137);
- System.out.println("After inserting integer value at offset 3 is: " + str1.toString());
- str1 = new StringBuffer("@!&*$$*&!@");
- System.out.println("Buffer is: " + str1);
- str1.insert(1, 78).insert(7, 258).insert(14, 87);
- System.out.println("After inserting integer value at offset 1,7 & 14 is: " + str1.toString());
- System.out.println();
- System.out.println("Output for insert(int offset, long lng):");
- System.out.println();
- StringBuffer str2 = new StringBuffer("Call me at anytime");
- System.out.println("Buffer is: " + str2);
- str2.insert(11, 879129217);
- System.out.println("After inserting long value at offset 11 is: " + str2.toString());
- str2 = new StringBuffer(" OR ");
- System.out.println("Buffer is: " + str2);
- str2.insert(0, 987431378).insert(13, 979487639);
- System.out.println("After inserting long value at offset 0 & 13 is: " + str2.toString());
- System.out.println();
- System.out.println("Output for insert(int offset, float f):");
- System.out.println();
- StringBuffer str3 = new StringBuffer("Rate is per item");
- System.out.println("Buffer is: " + str3);
- str3.insert(8, 3.14);
- System.out.println("After inserting float value at offset 8 is: " + str3.toString());
- str3 = new StringBuffer("Min rate & max rate ");
- System.out.println("Buffer is: " + str3);
- str3.insert(9, 81.4).insert(25, 314.5);
- System.out.println("After inserting float value at offset 9 & 25 is: " + str3.toString());
- System.out.println();
- System.out.println("Output for insert(int offset, double d):");
- System.out.println();
- StringBuffer str4 = new StringBuffer("Pay amount");
- System.out.println("Buffer is: " + str4);
- str4.insert(4, 796594.8524);
- System.out.println("After inserting double value at offset 4 is: " + str4.toString());
- str4 = new StringBuffer("Total is + ");
- System.out.println("Buffer is: " + str4);
- str4.insert(9, 67633.87).insert(20, 213576.346);
- System.out.println("After inserting double value at offset 9 & 13 is: " + str4.toString());
- System.out.println();
- System.out.println("Output for insert(int offset, double d):");
- System.out.println();
- StringBuffer str5 = new StringBuffer("You are ");
- System.out.println("Buffer is: " + str5);
- str5.insert(8, true);
- System.out.println("After inserting boolean value at offset 8 is: " + str5.toString());
- str5 = new StringBuffer("You both are and else are ");
- System.out.println("Buffer is: " + str5);
- str5.insert(13, false).insert(32, true);
- System.out.println("After inserting boolean value at offset 13 & 32 is: " + str5.toString());
- }
- }
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.