String To StringBuilder Java

String To StringBuilder Java | In this post, we will discuss how to convert string to StringBuilder. Previously we have seen how to convert StringBuilder to String using the toString() method and String class constructor.

The StringBuilder class allows us to modify the string literal without creating a new StringBuilder object. And StringBuilder class also contains some string manipulating methods that which String class doesn’t have like the reverse() method. To use those methods we can convert String to StringBuilder and call those methods. The StringBuilder was introduced in Java 5 which has made programmers’ tasks easy in order to manipulate strings.

How To Convert String To StringBuilder In Java

To convert String to StringBuilder object we can take the help of the StringBuilder class constructor. The following constructor can be used to convert String to StringBuilder:- public StringBuilder(String str). It constructs a StringBuilder initialized to the contents of the specified string. The initial capacity of the StringBuilder is 16 plus the length of the string argument.

public class Main {
   public static void main(String[] args) {
      String string = "Java Programming";
      System.out.println("String: " + string);
      System.out.println("Type: " + string.getClass().getName());

      // convert String to StringBuilder
      StringBuilder sb = new StringBuilder(string);
      System.out.println("StringBuilder: " + sb);
      System.out.println("Type: " + sb.getClass().getName());
   }
}

Output:-

String: Java Programming
Type: java.lang.String
StringBuilder: Java Programming
Type: java.lang.StringBuilder

Java String To StringBuilder Example

Let us see another example of String to StringBuilder conversion in Java using the StringBuilder class constructor. This time we will call some methods of the StringBuilder class for demonstration.

public class Main {
   public static void main(String[] args) {
      String string = "Enjoy each and every moment in life.";
      StringBuilder sb = new StringBuilder(string);

      System.out.println("StringBuilder: " + sb);
      System.out.println("Capacity: " + sb.capacity());
      System.out.println("Modified: " + sb.delete(6, 15));
      System.out.println("Reverse: " + sb.reverse());
   }
}

Output:-

StringBuilder: Enjoy each and every moment in life.
Capacity: 52
Modified: Enjoy every moment in life.
Reverse: .efil ni tnemom yreve yojnE

Now let us convert the string array to the StringBuilder. First, we will create a string array and then create a StringBuilder object by appending each element. The append() method inserts elements to the StringBuilder.

public class Main {
   public static void main(String[] args) {
      String string1[] = 
        { "Enjoy", " each", " and", " every", " moment", " in", " life" };
      StringBuilder sb = new StringBuilder();
      for (String string : string1) {
         sb.append(string);
      }
      System.out.println(sb.toString());
   }
}

Output:-

Enjoy each and every moment in life

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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