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.

Example

Output

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.

Example

Output

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.

References:
StringBuilder JavaDoc
StringBuffer JavaDoc

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.