TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# StringWriter

C# StringWriter for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# StringWriter Class

This class is used to write and deal with string data rather than files. It is derived class of TextWriter class. The string data written by StringWriter class is stored into StringBuilder.

The purpose of this class is to manipulate string and save result into the StringBuilder.

StringWriter Class Signature

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringWriter : TextWriter

C# StringWriter Constructors

Constructors Description
StringWriter() It is used to initialize a new instance of the StringWriter class.
StringWriter(IFormatProvider) It is used to initialize a new instance of the StringWriter class with the specified format control.
StringWriter(StringBuilder) It is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder.
StringWriter(StringBuilder,?IFormatProvider) It is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

C# StringWriter Properties

Property Description
Encoding It is used to get the Encoding in which the output is written.
FormatProvider It is used to get an object that controls formatting.
NewLine It is used to get or set the line terminator string used by the current TextWriter.

C# StringWriter Methods

Methods Description
Close() It is used to close the current StringWriter and the underlying stream.
Dispose() It is used to release all resources used by the TextWriter object.
Equals(Object) It is used to determine whether the specified object is equal to the current object or not.
Finalize() It allows an object to try to free resources and perform other cleanup operations.
GetHashCode() It is used to serve as the default hash function.
GetStringBuilder() It returns the underlying StringBuilder.
ToString() It returns a string containing the characters written to the current StringWriter.
WriteAsync(String) It is used to write a string to the current string asynchronously.
Write(Boolean) It is used to write the text representation of a Boolean value to the string.
Write(String) It is used to write a string to the current string.
WriteLine(String) It is used to write a string followed by a line terminator to the string or stream.
WriteLineAsync(String) Writes a string followed by a line terminator asynchronously to the current string.(Overrides TextWriter.WriteLineAsync(String).)

C# StringWriter Example

In the following program, we are using StringWriter class to write string information to the StringBuilder class. The StringReader class is used to read written information to the StringBuilder.

using System;
using System.IO;
using System.Text;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "Hello, Welcome to the TheDeveloperBlog \n" +
                "It is nice site. \n" +
                "It provides technical tutorials";
            // Creating StringBuilder instance
            StringBuilder sb = new StringBuilder();
            // Passing StringBuilder instance into StringWriter
            StringWriter writer = new StringWriter(sb);
            // Writing data using StringWriter
            writer.WriteLine(text);
            writer.Flush();
            // Closing writer connection
            writer.Close();
            // Creating StringReader instance and passing StringBuilder
            StringReader reader = new StringReader(sb.ToString());
            // Reading data
            while (reader.Peek() > -1)
            {
                Console.WriteLine(reader.ReadLine());
            }
        }
    }
}

Output:

Hello, Welcome to the TheDeveloperBlog
It is nice site.
It provides technical tutorials

Next TopicC# StringReader




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf