 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. 
Example
In the following Java program we are converting an array of Strings to a single String using the toString() method of the StringBuilder.
public class StringToStringBuilder { public static void main(String args[]) { String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" }; StringBuilder sb = new StringBuilder(); sb.append(strs[0]); sb.append(" "+strs[1]); sb.append(" "+strs[2]); sb.append(" "+strs[3]); sb.append(" "+strs[4]); sb.append(" "+strs[5]); String singleString = sb.toString(); System.out.println(singleString); } }
Output
Arshad Althamas Johar Javed Raju Krishna
Advertisements
                    