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

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

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

Java中CharArrayWriter类的toString()方法将给定的输入数据转换为字符串。

语法

public String[] toString()

参数:此方法不接受任何参数。

返回值:此方法返回输入数据的副本。

下面的程序说明了上述方法:

方案一:

Java
// Java program to illustrate the
// above mentioned method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Witer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
 
        CharSequence char_sq1 = "geeks for + ";
        CharSequence char_sq2 = "geeks";
 
        // Use of append(CharSequence char_sq)
        geek_writer1.append(char_sq1);
        geek_writer1.append(char_sq2);
 
        // Convert it into string and print
        System.out.println("String : "
                           + geek_writer1.toString());
    }
}


Java
// Java program to illustrate the
// above mentioned method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Witer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
 
        CharSequence char_sq1 = "Gopal ";
        CharSequence char_sq2 = "Dave";
 
        // Use of append(CharSequence char_sq)
        geek_writer1.append(char_sq1);
        geek_writer1.append(char_sq2);
 
        // Convert it into string and print
        System.out.println("String : "
                           + geek_writer1.toString());
    }
}


输出:
String : geeks for + geeks

方案二:

Java

// Java program to illustrate the
// above mentioned method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing String Witer
        CharArrayWriter geek_writer1
            = new CharArrayWriter();
 
        CharSequence char_sq1 = "Gopal ";
        CharSequence char_sq2 = "Dave";
 
        // Use of append(CharSequence char_sq)
        geek_writer1.append(char_sq1);
        geek_writer1.append(char_sq2);
 
        // Convert it into string and print
        System.out.println("String : "
                           + geek_writer1.toString());
    }
}
输出:
String : Gopal Dave

参考:https: Java/io/CharArrayWriter.html#toString()