How To Convert StringBuffer To String In Java

How To Convert StringBuffer To String In Java? In this blog, we convert the StringBuffer to string in Java. There are built-in methods available to convert the StringBuffer to a string object. First, we see the toString() method which directly converts the StringBuffer to a string. String class also contains a constructor to create a String object from the StringBuffer class object. Also see:- Java String To StringBuffer

How To Convert A StringBuffer To String In Java using the toString() Method

The toString() method of the StringBuffer class can be used to convert StringBuffer to String in Java. The syntax of the toString() method of StringBuffer class:- public String toString()
Parameters: There are no parameters accepted.
Returns: the string which is converted from a StringBuffer object.
Return type: string.

public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("KnowProgram");
        System.out.println("StringBuffer: " + sb);
        System.out.println(sb.getClass().getName());

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

Output:-

StringBuffer: KnowProgram
java.lang.StringBuffer
String: KnowProgram
java.lang.String

First, in the main class and in the main method we create an object of StringBuffer and pass the string “KnowProgram” to it. Then we convert the StringBuffer to the string object by using the toString() method and print the same using the print statement.

The getClass() method is used to find the current class and the getName() method returns the name of the class. In case of StringBuffer object getClass().getName() method return java.lang.StringBuffer and in the case of String object getClass().getName() method return java.lang.String.

How To Convert A StringBuffer To String In Java using the String Constructor

The String class has the following constructor to create a String object from the StringBuffer object:- public String(StringBuffer buffer)

It allocates a new string that contains the sequence of characters currently contained in the string buffer argument. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string.

public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello World");
        System.out.println("StringBuffer: " + sb);
        System.out.println(sb.getClass().getName());

        String string = new String(sb);
        System.out.println("String: " + string);
        System.out.println(string.getClass().getName());
    }
}

Output:-

StringBuffer: Hello World
java.lang.StringBuffer
String: Hello World
java.lang.String

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 *