StringWriter
Class
Definition
Implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
[System.Runtime.InteropServices.ComVisible(true)]
public class StringWriter : System.IO.TextWriter
- Inheritance
- Attributes
Inherited Members
System.IO.TextWriter
System.MarshalByRefObject
System.Object
Examples
The following code example demonstrates the creation of a continuous paragraph from a group of double-spaced sentences, and then the conversion of the paragraph back to the original text.
using namespace System;
using namespace System::IO;
int main()
{
String^ textReaderText = "TextReader is the abstract base "
"class of StreamReader and StringReader, which read "
"characters from streams and strings, respectively.\n\n"
"Create an instance of TextReader to open a text file "
"for reading a specified range of characters, or to "
"create a reader based on an existing stream.\n\n"
"You can also use an instance of TextReader to read "
"text from a custom backing store using the same "
"APIs you would use for a string or a stream.\n\n";
Console::WriteLine( "Original text:\n\n{0}", textReaderText );
// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
String^ aLine;
String^ aParagraph;
StringReader^ strReader = gcnew StringReader( textReaderText );
while ( true )
{
aLine = strReader->ReadLine();
if ( aLine != nullptr )
{
aParagraph = String::Concat( aParagraph, aLine, " " );
}
else
{
aParagraph = String::Concat( aParagraph, "\n" );
break;
}
}
Console::WriteLine( "Modified text:\n\n{0}", aParagraph );
// Re-create textReaderText from aParagraph.
int intCharacter;
Char convertedCharacter;
StringWriter^ strWriter = gcnew StringWriter;
strReader = gcnew StringReader( aParagraph );
while ( true )
{
intCharacter = strReader->Read();
// Check for the end of the string
// before converting to a character.
if ( intCharacter == -1 )
break;
convertedCharacter = Convert::ToChar( intCharacter );
if ( convertedCharacter == '.' )
{
strWriter->Write( ".\n\n" );
// Bypass the spaces between sentences.
strReader->Read();
strReader->Read();
}
else
{
strWriter->Write( convertedCharacter );
}
}
Console::WriteLine( "\nOriginal text:\n\n{0}", strWriter->ToString() );
}
using System;
using System.IO;
class StringRW
{
static void Main()
{
string textReaderText = "TextReader is the abstract base " +
"class of StreamReader and StringReader, which read " +
"characters from streams and strings, respectively.\n\n" +
"Create an instance of TextReader to open a text file " +
"for reading a specified range of characters, or to " +
"create a reader based on an existing stream.\n\n" +
"You can also use an instance of TextReader to read " +
"text from a custom backing store using the same " +
"APIs you would use for a string or a stream.\n\n";
Console.WriteLine("Original text:\n\n{0}", textReaderText);
// From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
aLine = strReader.ReadLine();
if(aLine != null)
{
aParagraph = aParagraph + aLine + " ";
}
else
{
aParagraph = aParagraph + "\n";
break;
}
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph);
// Re-create textReaderText from aParagraph.
int intCharacter;
char convertedCharacter;
StringWriter strWriter = new StringWriter();
strReader = new StringReader(aParagraph);
while(true)
{
intCharacter = strReader.Read();
// Check for the end of the string
// before converting to a character.
if(intCharacter == -1) break;
convertedCharacter = Convert.ToChar(intCharacter);
if(convertedCharacter == '.')
{
strWriter.Write(".\n\n");
// Bypass the spaces between sentences.
strReader.Read();
strReader.Read();
}
else
{
strWriter.Write(convertedCharacter);
}
}
Console.WriteLine("\nOriginal text:\n\n{0}",
strWriter.ToString());
}
}
Option Explicit
Option Strict
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Public Class StrReader
Shared Sub Main()
Dim textReaderText As String = "TextReader is the " & _
"abstract base class of StreamReader and " & _
"StringReader, which read characters from streams " & _
"and strings, respectively." & _
vbCrLf & vbCrLf & _
"Create an instance of TextReader to open a text " & _
"file for reading a specified range of characters, " & _
"or to create a reader based on an existing stream." & _
vbCrLf & vbCrLf & _
"You can also use an instance of TextReader to read " & _
"text from a custom backing store using the same " & _
"APIs you would use for a string or a stream." & _
vbCrLf & vbCrLf
Console.WriteLine("Original text:" & vbCrLf & vbCrLf & _
textReaderText)
' From textReaderText, create a continuous paragraph
' with two spaces between each sentence.
Dim aLine, aParagraph As String
Dim strReader As New StringReader(textReaderText)
While True
aLine = strReader.ReadLine()
If aLine Is Nothing Then
aParagraph = aParagraph & vbCrLf
Exit While
Else
aParagraph = aParagraph & aLine & " "
End If
End While
Console.WriteLine("Modified text:" & vbCrLf & vbCrLf & _
aParagraph)
' Re-create textReaderText from aParagraph.
Dim intCharacter As Integer
Dim convertedCharacter As Char
Dim strWriter As New StringWriter()
strReader = New StringReader(aParagraph)
While True
intCharacter = strReader.Read()
' Check for the end of the string
' before converting to a character.
If intCharacter = -1 Then
Exit While
End If
convertedCharacter = Convert.ToChar(intCharacter)
If convertedCharacter = "."C Then
strWriter.Write("." & vbCrLf & vbCrLf)
' Bypass the spaces between sentences.
strReader.Read()
strReader.Read()
Else
strWriter.Write(convertedCharacter)
End If
End While
Console.WriteLine(vbCrLf & "Original text:" & vbCrLf & _
vbCrLf & strWriter.ToString())
End Sub
End Class
Remarks
StringWriter enables you to write to a string synchronously or asynchronously. You can write a character at a time with the Write(Char) or the WriteAsync(Char) method, a string at a time using the Write(String) or the WriteAsync(String) method. In addition, you can write a character, an array of characters or a string followed by the line terminator asynchronously with one of the WriteLineAsync methods.
Note
This type implements the IDisposable interface, but does not actually have any resources to dispose. This means that disposing it by directly calling Dispose() or by using a language construct such as using (in C#) or Using (in Visual Basic) is not necessary.
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | How to: Write Text to a File |
| Write to a text file. | How to: Write Text to a File |
| Read from a text file. | How to: Read Text from a File |
| Append text to a file. | How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| Get the size of a file. | FileInfo.Length |
| Get the attributes of a file. | File.GetAttributes |
| Set the attributes of a file. | File.SetAttributes |
| Determine if a file exists. | File.Exists |
| Read from a binary file. | How to: Read and Write to a Newly Created Data File |
| Write to a binary file. | How to: Read and Write to a Newly Created Data File |
Constructors
| StringWriter() |
Initializes a new instance of the StringWriter class. |
| StringWriter(IFormatProvider) |
Initializes a new instance of the StringWriter class with the specified format control. |
| StringWriter(StringBuilder) |
Initializes a new instance of the StringWriter class that writes to the specified StringBuilder. |
| StringWriter(StringBuilder, IFormatProvider) |
Initializes a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider. |
Properties
| Encoding |
Gets the Encoding in which the output is written. |
Methods
| Close() |
Closes the current StringWriter and the underlying stream. |
| Dispose(Boolean) |
Releases the unmanaged resources used by the StringWriter and optionally releases the managed resources. |
| FlushAsync() |
Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device. |
| GetStringBuilder() |
Returns the underlying StringBuilder. |
| ToString() |
Returns a string containing the characters written to the current |
| Write(Char) |
Writes a character to the string. |
| Write(String) |
Writes a string to the current string. |
| Write(Char[], Int32, Int32) |
Writes a subarray of characters to the string. |
| WriteAsync(Char) |
Writes a character to the string asynchronously. |
| WriteAsync(String) |
Writes a string to the current string asynchronously. |
| WriteAsync(Char[], Int32, Int32) |
Writes a subarray of characters to the string asynchronously. |
| WriteLineAsync(Char) |
Writes a character followed by a line terminator asynchronously to the string. |
| WriteLineAsync(String) |
Writes a string followed by a line terminator asynchronously to the current string. |
| WriteLineAsync(Char[], Int32, Int32) |
Writes a subarray of characters followed by a line terminator asynchronously to the string. |