📜  Java中的 StringBuilder toString() 方法及示例

📅  最后修改于: 2022-05-13 01:55:29.097000             🧑  作者: Mango

Java中的 StringBuilder toString() 方法及示例

StringBuilder 类的toString()方法是用于返回表示 StringBuilder 对象包含的数据的字符串的内置方法。创建并初始化一个新的 String 对象,以从该 StringBuilder 对象中获取字符序列,然后由 toString() 返回 String。 Object 包含的此序列的后续更改不会影响 String 的内容。

句法:

public abstract String toString() ;

返回值:此方法总是返回一个字符串,表示 StringBuilder 对象包含的数据。

由于这是一个非常基本的方法,已被合并到Java中,因此我们将在干净的Java程序中讨论它以了解它的工作原理。在课堂内部,它的定义如下,这将使您更好地了解它的实际工作原理。

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

示例 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());
    }
}


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 to.String() method
        System.out.println("String contains = "
                           + str.toString());
    }
}


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);
    }
}


输出
String contains = GeeksForGeeks

示例 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 to.String() method
        System.out.println("String contains = "
                           + str.toString());
    }
}
输出
String contains = Geeks for Geeks contribute

现在我们完成了对基本示例的讨论,让我们通过使用 to.String() 方法将字符串数组转换为单个字符串来对字符串数组进行相同的操作。在这里,我们将创建一个 StringBuilder 类对象,然后将字符串数组存储为其对象。稍后,我们将创建另一个字符串并将所有元素放入其中。最后,我们将打印这个字符串。

示例 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);
    }
}
输出
Single string generated using toString() method is --> Are You A Programmer