Google+

148. Using 'getChars( )' method with StringBuffer







getChars( ) when used with StringBuffer will extract the substring from the specified string and copies the extracted substring into a char array.

Example:

StringBuffer object1 = new StringBuffer("SuperSunShine");
char targetCharArray[ ] = new char[3];

object1.getChars(5,8, targetCharArray, 0); -> This will extract the substring from the specified string "SuperSunShine" using the provided Start Index 5 and End Index (8-1) i.e. "Sun" in the specified string "SuperSunShine" and copies the extracted substring "Sun" to char array "tagetCharArray" from targetCharArray[0] i.e. 0 index.

Lets implement this on Eclipse IDE:

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



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



Download this project:

Click here to download this 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 append( ) method with StringBuffer will be explained in the next post.



No comments: