getChars( ) when used with StringBuffer will extract the substring from the specified string and copies the extracted substring into a char array.
StringBuffer object1 = new StringBuffer("SuperSunShine");
char targetCharArray[ ] = new char;
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 i.e. 0 index.
Lets implement this on Eclipse IDE:
1. Create 'getCharsDemo' class under any project as shown below:
3. Observe that the output is displayed in the console as shown below:
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.