Advertisement
Constructors | Description |
---|---|
StringWriter() | This constructor creates an object of the StringWriter that writes to its own created StringBuilder object. |
StringWriter(StringBuilder string) | The constructor created an object of the StringWriter that writes to the specified StringBuilder object. |
Methods | Description |
---|---|
void Write(char ch) | This method writes a character value to the stream. |
void Write(char[] array, int index, int total) | This method writes a character array to the stream, where:
|
void Write(String str) | This method writes a String to the stream. |
void Flush() | This method clears the buffer for the current writer stream by writing any buffered data to the stream. |
void Close() | This method closes this stream and also frees any resources connected with this stream. |
//C# Example of StringWriter() constructor
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//A character value
char ch = 'A';
//A character array
char[] arr = {'B', 'l', 'u', 'e', ' ', 'R' ,'a', 'i', 'n'};
//Creating a String object
String str = "There is light at the end of a tunnel";
//Creating an object of StringWriter
//to write to characters to the string of type StringBuilder
//automatically created by the StringWriter class
StringWriter sw = new StringWriter();
Console.WriteLine("Writing to the character to a string of type StringBuilder");
//Writing a char
sw.Write(ch);
//Writing a part of char array
//Starting at its zeroth-index and a total of 4 characters
sw.Write(arr, 0 , 7);
//Writing a String
sw.Write(str);
Console.WriteLine("Characters are successfully written to a string of type StringBuilder");
//Reading the content of string created by StringWriter class
Console.WriteLine("The content of string created by StringWriter class: "+ sw.ToString());
//Closing the stream i.e. StringWriter stream
//sw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the character to a string of type StringBuilder
Characters are successfully written to a string of type StringBuilder
The content of string created by StringWriter class: ABlue RaThere is light at the end of a tunnel
Advertisement
//C# Example of StringWriter(StringBuilder) constructor
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
try
{
//A character value
char ch = 'A';
//A character array
char[] arr = {'B', 'l', 'u', 'e', ' ', 'R' ,'a', 'i', 'n'};
//Creating a String object
String str = "There is light at the end of a tunnel";
//Creating an object of StringBuilder
StringBuilder sb = new StringBuilder();
//Creating an object of StringWriter
//to write to characters to the string of type StringBuilder
//which is specified in the constructor of StringBuilder class
StringWriter sw = new StringWriter(sb);
Console.WriteLine("Writing to the character to the specified string of type StringBuilder");
//Writing a char
sw.Write(ch);
//Writing a part of char array
//Starting at its zeroth-index and a total of 4 characters
sw.Write(arr, 0 , 7);
//Writing a String
sw.Write(str);
Console.WriteLine("Characters are successfully written to the specified string of type StringBuilder");
//Reading the content of string created by StringWriter class
Console.WriteLine("The content of string of StringBuilder created by StringWriter class: "+ sb.ToString());
//Closing the stream i.e. StringWriter stream
//sw.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the character to the specified string of type StringBuilder
Characters are successfully written to the specified string of type StringBuilder
The content of string of StringBuilder created by StringWriter class: ABlue RaThere is light at the end of a tunnel
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement