149. Using append( ) method with StringBuffer

append( ) method when used with StringBuffer concatenates the provided data to the end of the invoking StringBuffer object.


StringBuffer object1 = new StringBuffer("Start");
int num = 123;
String str = "abc";
StringBuffer object2 = new StringBuffer("Stop");

object1.append(num).append(456).append(str).append("xyz").append(object2);  -> This statement will append 123,456,abc,xyz and Stop to the "Start" text -> i.e. This will result Start123456abcxyzStop

Lets implement this on Eclipse IDE:

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

2. Save and Run the 'appendDemo' 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 'insert( )' method with StringBuffer will be explained in the next post.

No comments: