Advertisement



< Prev
Next >



C# StringWriter



C# provides us the StringWriter class which allows us to write characters to a String and this String is an object of of StringBuilder class, which is automatically created by the StringWriter class.

The StringWriter class implements the TextWriter abstract class and it is defined in the System.IO namespace.




Some commonly used constructors of StringWriter


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.






Some commonly used write methods of StringWriter


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:
  • index, represents the starting index in the char array to write to the output stream.
  • total, represents the total number of characters in char array to write to the output stream.


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.






In this example, we are going create an object of the StringWriter class by calling its different constructor, which is used to write characters to the specified string object of type StringBuilder class and not the automatically created string object of type StringBuilder class.

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


Output is -:


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



Program Analysis






Please share this article -




< Prev
Next >
< C# TextReader
C# StringReader >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement