StringReader StringReader StringReader StringReader Class

定義

實作TextReader,從字串讀取。Implements 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. 您可以使用一次讀取一個字元ReadReadAsync方法,在階段使用一行ReadLineReadLineAsync方法與整個字串使用ReadToEndReadToEndAsync方法。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.

注意

此類型會實作 IDisposable 介面,但實際上沒有任何要處置的資源。This type implements the IDisposable interface, but does not actually have any resources to dispose. 這表示其處置方式不一定要直接呼叫 Dispose() 或使用語言建構,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。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)

適用於

另請參閱