Java String Handling

Java Convert String to StringBuilder – StringBuilder to String example (StringBuffer)

This Java example shows how to covert String to StringBuilder and StringBuilder to String in Java. Example also shows how to convert String to StringBuffer and StringBuffer to String in java.

How to convert String to StringBuilder or StringBuffer?

There are many reasons you may want to convert String to StringBuilder or StringBuffer in Java. Since String is immutable (that means you cannot change the content of the String once it is created), you may want to convert it to mutable StringBuilder or StringBuffer if you want to append multiple values to it.

StringBuilder class provides a constructor which accepts String. You can use that constructor to convert String to StringBuilder object.

This constructor creates a new StringBuilder object having same characters as the parameter String.

Similarly, you can use constructor of the StringBuffer class to convert String to StringBuffer as given below.

This constructor creates a new StringBuffer object with same content as parameter String.



Note: The capacity of the newly created StringBuilder or StringBuffer object will be 16 + length of the String object.

How to convert StringBuilder to String (StringBuffer to String)?

It’s fairly easy. Use toString method of the StringBuilder or StringBuffer class to convert StringBuilder or StringBuffer to String object.



Visit Java StringBuffer examples & Java StringBuilder examples for more examples. You may also want to visit Java conversion tutorial to learn more about other conversions in Java.

StringBuilder JavaDoc
StringBuffer JavaDoc

About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *