Google+

150. Using 'insert( )' method with StringBuffer







insert( ) method when used with StringBuffer inserts one string into another string.

Example:

StringBuffer object1 = new StringBuffer("ABCDIJKL");
StringBuffer object2 = new StringBuffer("ABD");
StringBuffer object3 = new StringBuffer("ABEF");
String str = "EFGH";
char ch = 'C'
StringBuffer object4 = new StringBuffer("CD");

object1.insert(4, str); -> This will insert "EFGH" at index 4 in "ABCDIJKL". After inserting, the object1 will contain "ABCDEFGHIJKL" string.
object2.insert(2, ch); -> This will insert 'C' charater at index 2 in "ABD". After inserting, the object2 will contain "ABCD" string.
object3.insert(2, object4); -> This will insert "CD" at index 2 in "ABEF". After inserting, the object3 will contain "ABCDEF" string.

Lets implement this on Eclipse IDE:

1. Create 'insertDemo' class under any project as shown below:



2. Save and Run the 'insertDemo' class file
3. Observe that the output is displayed in the console as shown below:



Download this project:

Click here to download the project containing the class file used in this post (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

Using 'reverse( )' method with StringBuffer will be explained in the next post.




No comments:

Blog Index