StringReader Class

Definition

Implements a TextReader that reads from a string.

public ref class StringReader : System::IO::TextReader
public class StringReader : System.IO.TextReader
[System.Serializable]
public class StringReader : System.IO.TextReader
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StringReader : System.IO.TextReader
type StringReader = class
    inherit TextReader
[<System.Serializable>]
type StringReader = class
    inherit TextReader
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringReader = class
    inherit TextReader
Public Class StringReader
Inherits TextReader
Inheritance
StringReader
Inheritance
Attributes

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.

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

StringReader(String)

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

Methods

Close()

Closes the StringReader.

Close()

Closes the TextReader and releases any system resources associated with the TextReader.

(Inherited from TextReader)
CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the TextReader object.

(Inherited from TextReader)
Dispose(Boolean)

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

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
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(Span<Char>)

Reads all the characters from the input string, starting at the current position, and advances the current position to the end of the input string.

Read(Span<Char>)

Reads the characters from the current reader and writes the data to the specified buffer.

(Inherited from TextReader)
ReadAsync(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.

ReadAsync(Char[], Int32, Int32)

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

(Inherited from TextReader)
ReadAsync(Memory<Char>, CancellationToken)

Asynchronously reads all the characters from the input string, starting at the current position, and advances the current position to the end of the input string.

ReadAsync(Memory<Char>, CancellationToken)

Asynchronously reads the characters from the current stream into a memory block.

(Inherited from TextReader)
ReadBlock(Char[], Int32, Int32)

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

(Inherited from TextReader)
ReadBlock(Span<Char>)

Reads all the characters from the input string starting at the current position and advances the current position to the end of the input string.

ReadBlock(Span<Char>)

Reads the characters from the current stream and writes the data to a buffer.

(Inherited from TextReader)
ReadBlockAsync(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.

ReadBlockAsync(Char[], Int32, Int32)

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

(Inherited from TextReader)
ReadBlockAsync(Memory<Char>, CancellationToken)

Asynchronously reads all the characters from the input string starting at the current position and advances the current position to the end of the input string.

ReadBlockAsync(Memory<Char>, CancellationToken)

Asynchronously reads the characters from the current stream and writes the data to a buffer.

(Inherited from TextReader)
ReadLine()

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

ReadLineAsync()

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

ReadLineAsync()

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

(Inherited from TextReader)
ReadLineAsync(CancellationToken)

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

ReadLineAsync(CancellationToken)

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

(Inherited from TextReader)
ReadToEnd()

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

ReadToEndAsync()

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

ReadToEndAsync()

Reads all characters from the current position to the end of the text reader asynchronously and returns them as one string.

(Inherited from TextReader)
ReadToEndAsync(CancellationToken)

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

ReadToEndAsync(CancellationToken)

Reads all characters from the current position to the end of the text reader asynchronously and returns them as one string.

(Inherited from TextReader)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IDisposable.Dispose()

For a description of this member, see Dispose().

(Inherited from TextReader)

Applies to

See also