Learn StringBuffer Class in Java: Lecture 5

This article continues with the other insert() methods discussed in a previous lecture.

Before reading further, here are the previous parts of this series.

Now, proceed forward to learn more about insert() methods.

Other insert() methods are shown below.



Java StringBuffer insert(int offset, char c) inserts the string representation of the char argument into the given sequence. The value of 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 IndexOutOfBoundsException(), if the offset value in not valid.

Java StringBuffer insert(int offset, char c) inserts the string representation of the char array argument into the given sequence. The value of the offset argument should be greater than or equal to 0 (zero) or less than or equal to length of the given sequence. The characters of the array argument are inserted into the content of the given sequence at the position indicated by the offset value and the length of the sequence increases by the length of the argument. It throws StringIndexOutOfBoundsException() if the offset value is not valid.

Java StringBuffer insert(int index, char[] str, int offset, int len) inserts a string representation of a subarray of the string str argument into the given sequence. The subarray lies between the specified offset value and length len of the characters. The characters of the subarray are inserted into the sequence at the position indicated by the index value and the length of the sequence increases by len chars.

It also throws StringIndexOutOfBoundsException(), if the index value is negative or greater than the length of the string or the offset or len value is negative or offset + len is greater than the string length.

Example

Here all three of those methods are described in the following example.
  1. public class InsertMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer str1 = new StringBuffer("Insertion");  
  4.         System.out.println("Output for insert(int offset, char c):");  
  5.         System.out.println();  
  6.         System.out.println("Buffer is: " + str1);  
  7.         str1.insert(9's'); //insert char value at offset 9  
  8.         System.out.println("After inserting char value at offset 9 is: " + str1.toString()); //prints StringBuffer after inserting  
  9.         str1.insert(2'n');  
  10.         System.out.println("After inserting char value at offset 2 & 9 is: " + str1.toString());  
  11.         str1 = new StringBuffer("876512434");  
  12.         System.out.println("Buffer is: " + str1);  
  13.         str1.insert(0'9');  
  14.         System.out.println("After inserting char value at offset 0 is: " + str1.toString());  
  15.         System.out.println();  
  16.         System.out.println("Output for insert(int offset, char[] str):");  
  17.         System.out.println();  
  18.         StringBuffer str2 = new StringBuffer("Read this ");  
  19.         System.out.println("Buffer is: " + str2);  
  20.         char[] arr1 = {  
  21.             'l''e''c''t''u''r''e'  
  22.         }; //character array to be inserted  
  23.         str2.insert(10, arr1); //insert char array at offset 10  
  24.         System.out.println("After inserting char array value at offset 10 is: " + str2.toString()); //prints StringBuffer after inserting  
  25.         str2 = new StringBuffer("123789");  
  26.         char[] arr2 = {  
  27.             '4''5''6'  
  28.         };  
  29.         System.out.println("Buffer is: " + str2);  
  30.         str2.insert(3, arr2);  
  31.         System.out.println("After inserting char array value at offset 3 is: " + str2.toString());  
  32.         System.out.println();  
  33.         System.out.println("Output for insert(int index, char[] str, int offset, int len):");  
  34.         System.out.println();  
  35.         StringBuffer str3 = new StringBuffer("Happy day");  
  36.         System.out.println("Buffer is: " + str3);  
  37.         char[] arr3 = {  
  38.             'b''i''r''t''h'  
  39.         }; //character array to be inserted  
  40.         str3.insert(6, arr3, 05); //insert char array at index 6,offset 0 and len 5  
  41.         System.out.println("After inserting char array value at index 6,offset 0 and len 5 is: " + str3.toString()); //prints StringBuffer after inserting  
  42.         str3 = new StringBuffer("#@@#");  
  43.         char[] arr4 = {  
  44.             '1''4''3'  
  45.         };  
  46.         System.out.println("Buffer is: " + str3);  
  47.         str3.insert(2, arr4, 03);  
  48.         System.out.println("After inserting char array value at index 2,offset 0 and len 3 is: " + str3.toString());  
  49.         str3 = new StringBuffer("Good ment");  
  50.         char[] arr5 = {  
  51.             'a''c''k''n''o''w''l''e''d''g''e'  
  52.         };  
  53.         System.out.println("Buffer is: " + str3);  
  54.         str3.insert(5, arr5, 29); //insert char array at index 5,offset 2 and len 9  
  55.         System.out.println("After inserting char array value at index 5,offset 2 and len 9 is: " + str3.toString());  
  56.         /*offset 2 means it insert the 
  57. char array leaving 'a' & 'c' i.e. knowledge*/  
  58.     }  
  59. }  
Output



The following methods will be discussed now.



insert(int dstOffset, CharSequence cs) inserts the specified character sequence into the given sequence. The characters of the CharSequence are inserted in order (thread safe) into the given sequence at the specified offset value, moving up until any the charactes originally above that position and the length of the sequence is increased by the length of the argument s. It throws IndexOutOfBoundsException() if the offset value is invalid.

insert(int dstOffset, CharSequence cs, int start, int end) inserts a subsequence of the specified character sequence into the given sequence. The subsequence of the argument s are specified by start and end values that are inserted in the order (thread safe) into the given sequence at the specified position indicated by the offset value, moving up until any character originally above that position and the length of sequence is increases by start end & value. It throws IndexOutOfBoundsException() if the dstOffset value is negative or greater than the length of the string or the start or end are negative or the start is greater than the end or the end is greater than the given string length.

Example

Here, the preceding methods are described in this example.
  1. public class InsertMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuffer str1 = new StringBuffer("Acknowledge");  
  4.         System.out.println("Output for insert(int dstOffset, CharSequence cs):");  
  5.         System.out.println();  
  6.         System.out.println("Buffer is: " + str1);  
  7.         CharSequence CharSeq1 = ("ment");  
  8.         str1.insert(11, CharSeq1); //insert charSequence at offset 11  
  9.         System.out.println("After inserting charSeq at offset 11 is: " + str1.toString()); //prints StringBuffer after inserting  
  10.         str1 = new StringBuffer("12434");  
  11.         System.out.println("Buffer is: " + str1);  
  12.         CharSequence CharSeq2 = ("!@#$%");  
  13.         str1.insert(0, CharSeq2);  
  14.         System.out.println("After inserting charSeq at offset 0 is: " + str1.toString());  
  15.         System.out.println();  
  16.         System.out.println("Output for insert(int dstOffset, CharSequence cs, int start, int end):");  
  17.         System.out.println();  
  18.         StringBuffer str2 = new StringBuffer("Insert");  
  19.         System.out.println("Buffer is: " + str2);  
  20.         CharSequence CharSeq3 = ("Methods");  
  21.         str2.insert(6, CharSeq3, 07); //insert charSequence at dstOffset 6,start 0 & end 7  
  22.         System.out.println("After inserting charSeq value at dstOffset 6,start 0 & end 7 is: " + str2.toString()); //prints StringBuffer after inserting  
  23.         str2 = new StringBuffer("insert");  
  24.         CharSequence CharSeq4 = ("Methods");  
  25.         str2.insert(6, CharSeq4, 02);  
  26.         System.out.println("After inserting charSeq value at dstOffset 6,start 0 & end 2 is: " + str2.toString());  
  27.     }  
  28. }  
Output



Other important methods of the StringBuffer class are discussed in next article.
 
Learn StringBuffer Class in Java: Lecture 6 

Thank you, keep learning and sharing.