String​Reader Class

Definition

Implements a TextReader that reads from a string.

[System.Runtime.InteropServices.ComVisible(true)]
public class StringReader : System.IO.TextReader
Inheritance
StringReader
Attributes

Inherited Members

Examples

The following example shows how to read an entire string asynchronously.

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadCharacters();
        }

        static async void ReadCharacters()
        {
            StringBuilder stringToRead = new StringBuilder();
            stringToRead.AppendLine("Characters in 1st line to read");
            stringToRead.AppendLine("and 2nd line");
            stringToRead.AppendLine("and the end");

            using (StringReader reader = new StringReader(stringToRead.ToString()))
            {
                string readText = await reader.ReadToEndAsync();
                Console.WriteLine(readText);
            }
        }
    }
}
// The example displays the following output:
//
// Characters in 1st line to read
// and 2nd line
// and the end
//
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        ReadCharacters()
    End Sub

    Async Sub ReadCharacters()
        Dim stringToRead = New StringBuilder()
        stringToRead.AppendLine("Characters in 1st line to read")
        stringToRead.AppendLine("and 2nd line")
        stringToRead.AppendLine("and the end")

        Using reader As StringReader = New StringReader(stringToRead.ToString())
            Dim readText As String = Await reader.ReadToEndAsync()
            Console.WriteLine(readText)
        End Using
    End Sub
End Module
' The example displays the following output:
'
' Characters in 1st line to read
' and 2nd line
' and the end
'

Remarks

StringReader enables you to read a string synchronously or asynchronously. You can read a character at a time with the Read or the ReadAsync method, a line at a time using the ReadLine or the ReadLineAsync method and an entire string using the ReadToEnd or the ReadToEndAsync method.

Important

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

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

String​Reader(​String)

Initializes a new instance of the StringReader class that reads from the specified string.

Methods

Close()

Closes the StringReader.

Dispose(​Boolean)

Releases the unmanaged resources used by the StringReader and optionally releases the managed resources.

Peek()

Returns the next available character but does not consume it.

Read()

Reads the next character from the input string and advances the character position by one character.

Read(​Char[], ​Int32, ​Int32)

Reads a block of characters from the input string and advances the character position by count.

Read​Async(​Char[], ​Int32, ​Int32)

Reads a specified maximum number of characters from the current string asynchronously and writes the data to a buffer, beginning at the specified index.

Read​Block​Async(​Char[], ​Int32, ​Int32)

Reads a specified maximum number of characters from the current string asynchronously and writes the data to a buffer, beginning at the specified index.

Read​Line()

Reads a line of characters from the current string and returns the data as a string.

Read​Line​Async()

Reads a line of characters asynchronously from the current string and returns the data as a string.

Read​To​End()

Reads all characters from the current position to the end of the string and returns them as a single string.

Read​To​End​Async()

Reads all characters from the current position to the end of the string asynchronously and returns them as a single string.