The Wayback Machine - https://web.archive.org/web/20240726033234/https://www.geeksforgeeks.org/stringbuilder-tostring-method-in-java-with-examples/
Open In App

StringBuilder toString() method in Java with Examples

Last Updated : 23 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString(). Subsequent changes to this sequence contained by Object do not affect the contents of the String.

Syntax: 

public abstract String toString() ;

Return Value: This method always returns a string representing the data contained by StringBuilder Object.

As this is a very basic method been incorporated in java so we will be discussing it within our clean java programs to get to know its working. INternally in the class, it is defined as follows which will give you a better understanding of how it actually works. 

return getClass().getName()+ "@" + Integer.toHexString(hashCode);

Example 1:

Java




// Java program to demonstrate toString() Method
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder("GeeksForGeeks");
 
        // Print and display the string
        // using standard toString() method
        System.out.println("String contains = "
                           + str.toString());
    }
}


Output

String contains = GeeksForGeeks

Example 2:

Java




// Java program to demonstrate toString() Method.
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a StringBuilder object with
        // a String pass as parameter
        StringBuilder str = new StringBuilder(
            "Geeks for Geeks contribute");
 
        // Print and display the string
        // using toString() method
        System.out.println("String contains = "
                           + str.toString());
    }
}


Output

String contains = Geeks for Geeks contribute

Now we are done with discussing basic examples let us operate the same over an array of strings by converting it to a single string using to.String() method. Here we will create a StringBuilder class object then we will store the array of string as its object. Later on, we will create another string and will throw all elements in this. Lastly, we will print this string.

Example 3:

Java




// Java Program to Convert Array of Strings to A String
// Using toString() method
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Random array of string as input
        String gfg[] = { "Are", "You", "A", "Programmer" };
 
        // Creating object of StringBuilder class
        StringBuilder obj = new StringBuilder();
 
        // Adding above arrays of strings to
        // Stringbuilder object
        obj.append(gfg[0]);
        obj.append(" " + gfg[1]);
        obj.append(" " + gfg[2]);
        obj.append(" " + gfg[3]);
 
        // Note if elements are more then
        // we will be using loops to append(add)
 
        // Creating a single string
        String str = obj.toString();
 
        // Print and display the above string
        // containing all strings as a single string
        // using toString() method
        System.out.println(
            "Single string generated using toString() method is --> "
            + str);
    }
}


Output

Single string generated using toString() method is --> Are You A Programmer


Previous Article
Next Article

Similar Reads

StringBuilder append() Method in Java With Examples
The java.lang.StringBuilder.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used by the passing of various types of arguments: StringBuilder append(boolean a) :The java.lang.StringBuilder.append(boolean a) is an inbuilt method in Java which is
13 min read
StringBuilder offsetByCodePoints() method in Java with Examples
The offsetByCodePoints() method of StringBuilder class returns the index within this String contained by StringBuilder that is offset from the index passed as parameter by codePointOffset code points. Unpaired surrogates lies between index and codePointOffset count as one code point each. Syntax: public int offsetByCodePoints(int index, int codePoi
2 min read
StringBuilder substring() method in Java with examples
In StringBuilder class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuilder class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The string returned by this method contains all character
3 min read
StringBuilder trimToSize() method in Java with Examples
The trimToSize() method of StringBuilder class is the inbuilt method used to trims the capacity used for the character sequence of StringBuilder object. If the buffer used by StringBuilder object is larger than necessary to hold its current sequence of characters, then this method is called to resize the StringBuilder object for converting this obj
2 min read
StringBuilder appendCodePoint() method in Java with Examples
The appendCodePoint(int codePoint) method of StringBuilder class is the inbuilt method used to append the string representation of the codePoint argument to this sequence. The argument is appended to this StringBuilder content and length of the object is increased by Character.charCount(codePoint). The effect is the same as if the int value in para
2 min read
StringBuilder lastIndexOf() method in Java with Examples
In StringBuilder class, there are two types of lastIndexOf() method depending upon the parameters passed to it. lastIndexOf(String str) The lastIndexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for last occurrence of passed substring as parameter. The last occurrence of the empty stri
4 min read
StringBuilder indexOf() method in Java with Examples
In StringBuilder class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for first occurrence of passed substring as parameter. If substring str is not present then -1 is retur
4 min read
Matcher appendTail(StringBuilder) method in Java with Examples
The appendTail(StringBuilder) method of Matcher Class behaves as a append-and-replace method. This method reads the input string and appends it to the given StringBuilder at the tail position. Syntax: public StringBuilder appendTail(StringBuilder builder) Parameters: This method takes a parameter builder which is the StringBuilder that stores the t
2 min read
Matcher appendReplacement(StringBuilder, String) method in Java with Examples
The appendReplacement(StringBuilder, String) method of Matcher Class behaves as a append-and-replace method. This method reads the input string and replace it with the matched pattern in the matcher string. Syntax: public Matcher appendReplacement(StringBuilder builder, String stringToBeReplaced) Parameters: This method takes two parameters: builde
3 min read
StringBuilder Class in Java with Examples
StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class, as it creates a mutable sequence of characters. The function of StringBuilder is very much similar to the StringBuffer class, as both of them
6 min read
Practice Tags :
three90RightbarBannerImg