In this article you will see, how to convert a StringBuilder to String in Java.
Introduction
Java StringBuilder
is non-synchronized update of the StringBuffer
class.
Sometimes there is need to convert the StringBuilder
object to the String
. We can do it easily by smply invoking the toString()
method.
Let see the example program below.
Java Program to convert StringBuilder
to String
/**
* A Java program to convert StringBuilder to String
* @author coderolls at cderlls.com
*
*/
public class StringBuilderToString {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("Hello ");
stringBuilder.append("Gaurav ")
.append("Kukade!");
System.out.println("Printing strinBuilder as String: \n");
System.out.println(stringBuilder.toString());
}
}
Output:
Printing strinBuilder as String:
Hello coderolls!
Get the above code as GitHub Gist.
You can visit my YouTube channel ‘coderolls’ to find more video tutorials.
PREVIOUSHow To Create UUID in Java?