StringReader StringReader StringReader StringReader Class

Definition

Implements a TextReader that reads from a string.

public ref class StringReader : System::IO::TextReader
[System.Runtime.InteropServices.ComVisible(true)]
public class StringReader : System.IO.TextReader
type StringReader = class
    inherit TextReader
Public Class StringReader
Inherits TextReader
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) StringReader(String) StringReader(String) StringReader(String)

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

Methods

Close() Close() Close() Close()

Closes the StringReader.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) 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() Dispose() Dispose() Dispose()

Releases all resources used by the TextReader object.

(Inherited from TextReader)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Peek() Peek() Peek() Peek()

Returns the next available character but does not consume it.

Read() Read() Read() Read()

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

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

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

Read(Span<Char>) Read(Span<Char>) Read(Span<Char>) Read(Span<Char>)
ReadAsync(Char[], Int32, Int32) ReadAsync(Char[], Int32, Int32) ReadAsync(Char[], Int32, Int32) 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(Memory<Char>, CancellationToken) ReadAsync(Memory<Char>, CancellationToken) ReadAsync(Memory<Char>, CancellationToken) ReadAsync(Memory<Char>, CancellationToken)
ReadBlock(Char[], Int32, Int32) ReadBlock(Char[], Int32, Int32) ReadBlock(Char[], Int32, Int32) 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>) ReadBlock(Span<Char>) ReadBlock(Span<Char>) ReadBlock(Span<Char>)
ReadBlockAsync(Char[], Int32, Int32) ReadBlockAsync(Char[], Int32, Int32) ReadBlockAsync(Char[], Int32, Int32) 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(Memory<Char>, CancellationToken) ReadBlockAsync(Memory<Char>, CancellationToken) ReadBlockAsync(Memory<Char>, CancellationToken) ReadBlockAsync(Memory<Char>, CancellationToken)
ReadLine() ReadLine() ReadLine() ReadLine()

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

ReadLineAsync() ReadLineAsync() ReadLineAsync() ReadLineAsync()

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

ReadToEnd() ReadToEnd() ReadToEnd() ReadToEnd()

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

ReadToEndAsync() ReadToEndAsync() ReadToEndAsync() ReadToEndAsync()

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

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() Inherited from TextReader

Applies to

See Also