The toString() method of the CharArrayWriter class in Java converts the given input data to a string.
Syntax:
public String[] toString()
Parameters: This method does not accept any parameter.
Return Value: This method returns a copy of the input data.
Below program illustrate the above method:
Program 1:
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());
}
} |
Output:
String : geeks for + geeks
Program 2:
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());
}
} |
Output:
String : Gopal Dave
Reference: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#toString()