StringReader StringReader StringReader StringReader Class

定义

实现从字符串进行读取的 TextReaderImplements a TextReader that reads from a string.

public ref class StringReader : System::IO::TextReader
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class StringReader : System.IO.TextReader
type StringReader = class
    inherit TextReader
Public Class StringReader
Inherits TextReader
继承
属性

示例

下面的示例演示如何以异步方式读取整个字符串。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
'

注解

StringReader使您能够同步或异步读取字符串。StringReader enables you to read a string synchronously or asynchronously. 您可以使用或方法, Read一次ReadAsync读取一个字符, ReadLine使用ReadLineAsync或方法并使用或方法, 一次读取一个字符串ReadToEndReadToEndAsyncYou 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.

备注

此类型可实现 IDisposable 接口,但实际上没有任何要释放的资源。This type implements the IDisposable interface, but does not actually have any resources to dispose. 这意味着无需通过直接调用 Dispose() 或使用语言构造(C# 中的 using 或 Visual Basic 中的 Using)对其进行处理。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.

下表列出了其他典型或相关 i/o 任务的示例。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

构造函数

StringReader(String) StringReader(String) StringReader(String) StringReader(String)

初始化从指定字符串进行读取的 StringReader 类的新实例。Initializes a new instance of the StringReader class that reads from the specified string.

方法

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

关闭 StringReaderCloses 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()

释放由 TextReader 对象使用的所有资源。Releases all resources used by the TextReader object.

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

释放由 StringReader 占用的非托管资源,还可以另外再释放托管资源。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()

获取当前实例的 TypeGets 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()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

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

创建当前 MarshalByRefObject 对象的浅表副本。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)

读取输入字符串中的字符块,并将字符位置提升 countReads 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)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

有关此成员的说明,请参见 Dispose()For a description of this member, see Dispose().

(Inherited from TextReader)

适用于

另请参阅