How to Convert String to StringBuilder and vice versa in Java?

In Java, Strings are objects which represents the array of character. Strings are immutable in java , that means whenever you do any changes in a string an entirely new string is created.

This property of strings in java make it slow , hence we use StringBuilders instead of String if we need to speed up our string manipulation in java. StringBuilders are nothing but mutable version of Strings or you can say modified version of Strings which were introduced with Java 5 version.

There is also a StringBuffer class and both the StringBuffer and StringBuilder class works same and has same property but the main difference between the two is that StringBuilders are not thread safe i.e. not synchronized unlike StringBuffer.

StringBuilder class are always prefered to be used as it is faster than StringBuffer and way faster than Strings. However, if the thread safety is necessary , you should choose StringBuffer in that case.

Now, let’s learn how you can convert String to StringBuilder and vice-versa. This article will help you converting your String into StringBuilder or StringBuilder to String which will speed up the execution of your code.

String to StringBuilder conversion in Java

In StringBuilder class we can use the append() function which accepts String value and adds it up to the current object.

Code

     String s = "hello coder!";
     StringBuilder sb = new StringBuilder();
     sb.append(s);

Now, lets take an example of a code converting a string array into a single StringBuilder object.

Code

    public class StringToStringBuilder {
      public static void main(String args[]) {
        String strs[] = {"Hello", "coder", "aur", "launde", "kaam", "kareho?" };
        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

 Hello coder aur launde kaam kareho?

This is one more simple way of creating a stringbuilder object from string. See this code as example ,

code

   String string = "Hello, kaam karo!"
   StringBuilder sb = new StringBuilder(string);

So, now you have learned how to convert a string into StringBuilder , let’s see how to do the vice-versa i.e. How to convert StringBuilder to String ?

StringBuilder to String conversion in Java

To convert a StringBuilder object into String , you can use the toString() method provided by the StringBuilder class. The toString() method of the StringBuilder class returns String value of the current object. To convert the StringBuilder into String we can simply invoke the toString() method.

Let’s see through the code of the above example where we printed the stringbuilder by converting it into a String as System.out.println() method in Java cannot print StringBuilder objects,

code

   public class StringBuilderToString {
      public static void main(String args[]) {
        String strs[] = {"Hello", "coder", "aur", "launde", "kaam", "kareho?" };
        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 string = sb.toString();
        System.out.println(string);
     }
  }
    Hello coder aur launde kaam kareho?

Take a simple other example of StringBuilder to String conversion.

Code

   StringBuilder sb = new StringBuilder("Hello kaam karo bhai!");
   String advice = sb.toString();
   System.out.println(advice);

Output

    Hello kaam karo bhai!

Thankyou for learning and growing with www.codingbroz.com , we keep posting helpful articles everyday. Check other Articles too hope that also help you!

Leave a Comment

Your email address will not be published. Required fields are marked *