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

Java Program to check a String is palindrome or not by ignoring its case


  • A palindrome is a word that reads the same backward or forward.
  • Lets see what if the characters in a palindrome string are in different case. i.e should not be case sensitive. 
  • So now we will see how to check a string is palindrome or not by ignoring its case.
  • Write a function which checks  that string is palindrome or not by converting the original string to lowercase or uppercase.
  • Method should return true if it is palindrome, return false if not a palindrome.


#1 : Java Program to check given string is palindrome or not by ignoring its case.

  1. package com.instanceofjava;

  2. public class CheckPalindrome {
  3. public static boolean isPalindrome(String str) {
  4. StringBuffer strone=new StringBuffer(str.toLowerCase());
  5. StringBuffer strtwo=new StringBuffer(strone);
  6.  
  7.   strone.reverse();
  8.  
  9.   System.out.println("Orginal String ="+strtwo);
  10.   System.out.println("After Reverse ="+strone);
  11.  
  12. if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  13. return true;
  14.     else
  15.     return false;
  16. }
  17. public static void main(String[] args) {
  18. boolean ispalindrome= isPalindrome("DeleveleD");
  19. System.out.println(ispalindrome);
  20.  
  21.     }

  22. }

Output

  1. Orginal String =deleveled
  2. After Reverse =deleveled
  3. true

check palindrome or not ignore case sensitive
Select Menu