Converting a StringBuilder to String in Java

The toString() method of the StringBuilder class reruns String value of the current object. To convert a StringBuilder to String value simple invoke the toString() method on it.

  • Instantiate the StringBuilder class.

  • Append data to it using the append() method.

  • Convert the StringBuilder to string using the toString() method.


In the following Java program we are converting an array of Strings to a single String using the toString() method of the StringBuilder.

 Live Demo

public class StringToStringBuilder {
   public static void main(String args[]) {
      String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };
      StringBuilder sb = new StringBuilder();
      sb.append(" "+strs[1]);
      sb.append(" "+strs[2]);
      sb.append(" "+strs[3]);
      sb.append(" "+strs[4]);
      sb.append(" "+strs[5]);
      String singleString = sb.toString();


Arshad Althamas Johar Javed Raju Krishna

Updated on: 02-Sep-2023

51K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started