StringBuilder toString() Java

StringBuilder toString() Java | In this post, we will discuss the toString() method in the StringBuilder class. Compared to the Java String class there are only a few methods in the StringBuilder class. Hence to use String class methods for the StringBuilder object we have to convert the StringBuilder Object to String.

StringBuilder class contains the toString() method which can be used to convert StringBuilder to String. Once StringBuilder is converted to the String then the methods of the string class can be used to manipulate the data of the StringBuilder object.

The toString() method of the StringBuilder class does not take any parameters, it just converts the given StringBuilder object to String type. The syntax for the StringBuilder toString() method is as follows:- public String toString()
Parameters: It does not accept any parameters.
Return type: String.
Returns: the converted String.

Java StringBuilder toString() Example

In the below program we have created a StringBuilder object and then converted it to a string by using the toString() method, and displayed the value with its type.

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

      // calling toString() method
      String string = sb.toString();
      System.out.println("String: " + string);
      System.out.println("Type: " + string.getClass().getName());
   }
}

Output:-

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

Content-wise both String and StringBuilder will contain the same data. But we can get their type by using the getClass().getName() method which will return the type of the class. Let us see one more example of StringBuilder toString() Java.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("KnowProgram");
      String string = sb.toString();
      System.out.println("String: " + string);
      System.out.println("Length: " + string.length());
      System.out.println("After Replacement: " + string.replace('o', '@'));
      System.out.println("Repeat: " + string.repeat(2));
   }
}

Output:-

String: KnowProgram
Length: 11
After Replacement: Kn@wPr@gram
Repeat: KnowProgramKnowProgram

How To Change StringBuilder toString In Java

Now we will convert an array of strings in the StringBuilder class to a string. Here we first create an array of strings and then create a StringBuilder object later we append the array to the StringBuilder and then use the toString method to get the string representation of the StringBuilder object.

public class Main {
   public static void main(String[] args) {
      String array[] = { "Welcome", "to", "Java", "Programming", "Language" };
      StringBuilder sb = new StringBuilder();
      sb.append(array[0]);
      sb.append(" " + array[1]);
      sb.append(" " + array[2]);
      sb.append(" " + array[3]);
      sb.append(" " + array[4]);

      String string = sb.toString();
      System.out.println("String: " + string);
   }
}

Output:-

String: Welcome to Java Programming Language

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 *