Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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 String to StringBuilder in Java
The append() method of the StringBuilder class accepts a String value and adds it to the current object.
To convert a String value to StringBuilder object −
Get the string value.
Append the obtained string to the StringBuilder using the append() method.
Example
In the following Java program, we are converting an array of Strings to a single StringBuilder object.
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]);
System.out.println(sb.toString());
}
}
Output
Arshad Althamas Johar Javed Raju Krishna
Advertisements