This Java example shows how to convert String to StringBuilder and StringBuilder to String in Java. The 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 the 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.
The StringBuilder class provides a constructor that accepts String. You can use that constructor to convert String to StringBuilder object.
1 |
public StringBuilder(CharSequence sequence) |
This constructor creates a new StringBuilder object having the same characters as the specified parameter String.
Similarly, we can use the constructor of the StringBuffer class to convert String to StringBuffer as given below.
1 |
public StringBuffer(CharSequence sequence) |
This constructor creates a new StringBuffer object with the same content as the parameter String.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.javacodeexamples.stringbuilderexamples; public class ConvertStringToStringBuilderExample { public static void main(String[] args) { String str = "String to StringBuilder StringBuffer"; /* * To convert String to StringBuilder/StringBuffer * use the constructor. * * public StringBuilder(String str) * OR * public StringBuffer(String str) */ StringBuilder sbl = new StringBuilder(str); System.out.println("StringBuilder contents: " + sbl); StringBuffer sbf = new StringBuffer(str); System.out.println("StringBuffer contents: " + sbf); } } |
Output
1 2 |
StringBuilder contents: String to StringBuilder StringBuffer StringBuffer contents: String to StringBuilder StringBuffer |
Note: The capacity of the newly created StringBuilder or StringBuffer object will be 16 + the length of the String object.
How to convert StringBuilder to String (StringBuffer to String)?
It’s fairly easy. We can use the toString
method of the StringBuilder or StringBuffer class to convert StringBuilder or StringBuffer to a String object.
1 |
public String toString() |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javacodeexamples.stringbuilderexamples; public class ConvertStringToStringBuilderExample { public static void main(String[] args) { StringBuilder sbl = new StringBuilder("I am StringBuilder"); String str1 = sbl.toString(); System.out.println("StringBuilder to String: " + str1); StringBuffer sbf = new StringBuffer("I am StringBuffer"); String str2 = sbf.toString(); System.out.println("StringBuffer to String: " + str2); } } |
Output
1 2 |
StringBuilder to String: I am StringBuilder StringBuffer to String: I am StringBuffer |
You may also want to visit the Java conversion tutorial to learn more about other conversions in Java.
This example is a part of the Java StringBuffer tutorial and Java StringBuilder tutorial.
References:
StringBuilder JavaDoc
StringBuffer JavaDoc