» » How to Convert string to StringBuilder and vise versa in Java


  • We can Covert String to string builder in two ways
  1. Using constructor of StringBuilder(String s)
  2. Using append method of StringBuilder 
  • Lets see a java program to convert java String object to StringBuilder using constructor of StringBuilder class.

#1: Java program to convert string to StringBuilder using constructor of StringBuilder class.



  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: Java Program to convert String to StringBuilder
  7.  *
  8.  */
  9. public class StringBuilderDemo {

  10. public static void main(String[] args) {
  11. String str="java";
  12. StringBuilder sb = new StringBuilder(str);
  13. String s = sb.toString();
  14. System.out.println(s);
  15. StringBuilder stebldr= new StringBuilder("convert string to stringbuilder");
  16. String st = stebldr.toString();
  17. System.out.println(st);
  18. }

  19. }

Output

  1. java
  2. convert string to stringbuilder

#2: Java program to convert string to StringBuilder using append method of StringBuilder class.
  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: Java Program to convert String to StringBuilder
  7.  *
  8.  */
  9. public class StringBuilderDemo {

  10. public static void main(String[] args) {

  11. StringBuilder sb = new StringBuilder();
  12. sb.append("Java convert string to StringBuilder ");
  13. System.out.println(sb);
  14. String str= "Convert string to Stringbuilder" ;
  15. sb.append(str);
  16. System.out.println(sb);
  17. }

  18. }


Output

  1. Java convert string to StringBuilder 
  2. Java convert string to StringBuilder Convert string to Stringbuilder

  • We can Convert StringBuilder to String by using toString() method of StringBuilder class.
  • How to convert StringBuilder to String in java.


#3: Java program to convert string to StringBuilder using append method of StringBuilder class.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: Java Program to convert StringBuilder to String
  7.  *
  8.  */
  9. public class StringBuilderDemo {

  10. public static void main(String[] args) {

  11. StringBuilder sb = new StringBuilder();
  12. sb.append("Java convert  StringBuilder to string");
  13. String str=sb.toString();
  14. System.out.println(str);
  15. String s= "Convert Stringbuilder to string " ;
  16. sb.append(s);
  17. String st=sb.toString();
  18. System.out.println(st);
  19. }

  20. }

Output

  1. Java convert  StringBuilder to string
  2. Java convert  StringBuilder to stringConvert Stringbuilder to string 


java program to convert string to string builder

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
This is the most recent post.
»
Previous
Older Post

No comments

Leave a Reply

Select Menu