Google+

154. Using 'replace( )' method with StringBuffer







replace( ) method when used with StringBuffer replaces the set of characters with another set of characters.

Example:

StringBuffer object1 = new StringBuffer("I hate you");
object1.replace(2,6,"love");  -> Replaces the "hate" in "I love you" with "love". The object1 will contain "I love you" string after replacing.


Lets implement this on Eclipse IDE:


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

























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




No comments: