StringReader 類別

定義

實作讀取字串的 TextReader

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
繼承
StringReader
繼承
屬性

範例

下列範例示範如何以非同步方式讀取整個字串。

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 可讓您以同步或非同步方式讀取字串。 您可以使用 或 ReadAsync 方法一次 Read 讀取字元、使用 ReadLine 或 方法一次一行,以及使用 ReadToEndReadLineAsync ReadToEndAsync 方法讀取整個字串。

注意

此類型會實作 IDisposable 介面,但實際上沒有任何要處置的資源。 這表示其處置方式不一定要直接呼叫 Dispose() 或使用語言建構,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔案讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔案。 作法:讀取和寫入新建立的資料檔案

建構函式

StringReader(String)

初始化 StringReader 類別的新執行個體,這個執行個體會從指定的字串讀取。

方法

Close()

關閉 StringReader

Close()

關閉 TextReader 並釋放任何與 TextReader 關聯的系統資源。

(繼承來源 TextReader)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 TextReader 物件使用的所有資源。

(繼承來源 TextReader)
Dispose(Boolean)

釋放 StringReader 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已過時。

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
Peek()

傳回下一個可供使用的字元,但不使用它。

Read()

自輸入字串讀取下一個字元,並將字元位置前移一個字元。

Read(Char[], Int32, Int32)

自輸入字串讀取字元區塊,並將字元位置前移 count 個字元。

Read(Span<Char>)

從目前位置開始,讀取輸入字串中的所有字元,並將目前位置往前移至輸入字串的結尾。

Read(Span<Char>)

從目前的讀取器讀取字元,並將資料寫入至指定的緩衝區。

(繼承來源 TextReader)
ReadAsync(Char[], Int32, Int32)

從目前的字串非同步讀取指定的取大字元數目,並從指定的索引開始將資料寫入緩衝區。

ReadAsync(Char[], Int32, Int32)

從目前的文字讀取器非同步讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

(繼承來源 TextReader)
ReadAsync(Memory<Char>, CancellationToken)

從目前位置開始,以非同步方式讀取輸入字串中的所有字元,並將目前位置往前移至輸入字串的結尾。

ReadAsync(Memory<Char>, CancellationToken)

以非同步方式將目前資料流中的字元讀入至記憶體區塊。

(繼承來源 TextReader)
ReadBlock(Char[], Int32, Int32)

從目前的文字讀取器讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

(繼承來源 TextReader)
ReadBlock(Span<Char>)

從目前位置開始,讀取輸入字串中的所有字元,並將目前位置往前移至輸入字串的結尾。

ReadBlock(Span<Char>)

從目前的資料流讀取字元,並將資料寫入至緩衝區。

(繼承來源 TextReader)
ReadBlockAsync(Char[], Int32, Int32)

從目前的字串非同步讀取指定的取大字元數目,並從指定的索引開始將資料寫入緩衝區。

ReadBlockAsync(Char[], Int32, Int32)

從目前的文字讀取器非同步讀取指定的最大字元數目,並從指定的索引開始將資料寫入緩衝區。

(繼承來源 TextReader)
ReadBlockAsync(Memory<Char>, CancellationToken)

從目前位置開始,以非同步方式讀取輸入字串中的所有字元,並將目前位置往前移至輸入字串的結尾。

ReadBlockAsync(Memory<Char>, CancellationToken)

以非同步方式從目前的資料流讀取字元,並將資料寫入至緩衝區。

(繼承來源 TextReader)
ReadLine()

自目前字串讀取一行字元,並將資料以字串傳回。

ReadLineAsync()

自目前字串非同步讀取一行字元,並將資料以字串傳回。

ReadLineAsync()

非同步讀取一行字元,並將資料以字串傳回。

(繼承來源 TextReader)
ReadLineAsync(CancellationToken)

自目前字串非同步讀取一行字元,並將資料以字串傳回。

ReadLineAsync(CancellationToken)

非同步讀取一行字元,並將資料以字串傳回。

(繼承來源 TextReader)
ReadToEnd()

讀取從目前位置到字串末端的所有字元,並將它們以單一字串傳回。

ReadToEndAsync()

非同步讀取從目前位置到字串末端的所有字元,並將它們以單一字串傳回。

ReadToEndAsync()

非同步讀取從目前位置到文字讀取器末端的所有字元,並將它們以單一字串傳回。

(繼承來源 TextReader)
ReadToEndAsync(CancellationToken)

非同步讀取從目前位置到字串末端的所有字元,並將它們以單一字串傳回。

ReadToEndAsync(CancellationToken)

非同步讀取從目前位置到文字讀取器末端的所有字元,並將它們以單一字串傳回。

(繼承來源 TextReader)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

如需這個成員的說明,請參閱 Dispose()

(繼承來源 TextReader)

適用於

另請參閱