Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

StringReader Class

Definition

Namespace:
System.IO
Assemblies:
mscorlib.dll, System.IO.dll
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.IO.dll
Assembly:
System.Runtime.Extensions.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
StringReader.cs
Source:
StringReader.cs
Source:
StringReader.cs
Source:
StringReader.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Implements aTextReader 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 StringReaderInherits 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.IOImports System.TextModule 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 SubEnd 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 theRead or theReadAsync method, a line at a time using theReadLine or theReadLineAsync method and an entire string using theReadToEnd or theReadToEndAsync method.

Note

This type implements theIDisposable interface, but does not actually have any resources to dispose. This means that disposing it by directly callingDispose() or by using a language construct such asusing (in C#) orUsing (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

NameDescription
StringReader(String)

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

Methods

NameDescription
Close()

Closes theStringReader.

CreateObjRef(Type)

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

(Inherited fromMarshalByRefObject)
Dispose()

Releases all resources used by theTextReader object.

(Inherited fromTextReader)
Dispose(Boolean)

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

Equals(Object)

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

(Inherited fromObject)
GetHashCode()

Serves as the default hash function.

(Inherited fromObject)
GetLifetimeService()
Obsolete.

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

(Inherited fromMarshalByRefObject)
GetType()

Gets theType of the current instance.

(Inherited fromObject)
InitializeLifetimeService()
Obsolete.

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

(Inherited fromMarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the currentObject.

(Inherited fromObject)
MemberwiseClone(Boolean)

Creates a shallow copy of the currentMarshalByRefObject object.

(Inherited fromMarshalByRefObject)
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 bycount.

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.

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)

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.

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 fromTextReader)
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.

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)

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.

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(CancellationToken)

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

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(CancellationToken)

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

ToString()

Returns a string that represents the current object.

(Inherited fromObject)

Explicit Interface Implementations

NameDescription
IDisposable.Dispose()

For a description of this member, seeDispose().

(Inherited fromTextReader)

Applies to

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?