- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
- Related Articles
- Converting String to StringBuilder in Java
- Converting ArrayList to String[] in java
- Where to use a StringBuffer/StringBuilder than a String in Java?
- Java String, StringBuffer and StringBuilder Tutorial.
- How to convert String to StringBuilder and vice versa Java?
- Replace a string using StringBuilder
- Converting string to a binary string - JavaScript
- Converting a string to a date in JavaScript
- Converting string to date in MongoDB?
- Java StringBuilder class.
- Converting a string to NATO phonetic alphabets in JavaScript
- Difference between String and StringBuilder in C#
- Converting string to MORSE code in JavaScript
- Converting list string to dictionary in Python
- Converting whitespace string to url in JavaScript

Advertisements