StringReader 類別

定義

實作一個從字串讀取的 a 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 允許你同步或非同步讀取字串。 你可以用 Read or 方法一次讀一個字元,用 ReadLine or 方法ReadLineAsync一行讀,或用 or ReadToEndAsync 方法讀整串字串ReadToEndReadAsync

Note

此類型實 IDisposable 作介面,但實際上沒有資源可處置。 這表示不必直接呼叫 Dispose() 或使用語言結構,如 using(C#)或 Using(Visual Basic)來處理。

下表列出其他典型或相關的 I/O 任務範例。

若要這麼做... 請參閱本主題中的範例...
建立文字檔。 如何:將文字寫入檔案
寫入文字檔。 如何:將文字寫入檔案
從文字檔讀取。 如何:從檔案讀取文字
在檔案中附加文字。 如何:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
拿出檔案大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷是否有檔案存在。 File.Exists
從二進位檔案讀取。 如何:讀取和寫入新建立的數據檔
寫入二進位檔案。 如何:讀取和寫入新建立的數據檔

建構函式

名稱 Description
StringReader(String)

初始化一個新的類別實例 StringReader ,讀取指定的字串。

方法

名稱 Description
Close()

關閉 StringReader.

CreateObjRef(Type)

建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。

(繼承來源 MarshalByRefObject)
Dispose()

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

(繼承來源 TextReader)
Dispose(Boolean)

釋放 未管理的資源, StringReader 並可選擇性地釋放受管理資源。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()

取得目前控制此實例生命週期政策的終身服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前實例的 Type

(繼承來源 Object)
InitializeLifetimeService()

取得一個終身服務物件以控制此實例的終身政策。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立一個 MarshalByRefObject 目前物件的淺層複製品。

(繼承來源 MarshalByRefObject)
Peek()

返回下一個可用角色,但不會消耗該角色。

Read()

讀取輸入字串中的下一個字元,並將字元位置前進一個字元。

Read(Char[], Int32, Int32)

從輸入字串讀取一組字元,並將字元位置 count前進 。

Read(Span<Char>)

從輸入字串讀取所有字元,從當前位置開始,並將當前位置推進到輸入字串的末尾。

ReadAsync(Char[], Int32, Int32)

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

ReadAsync(Memory<Char>, CancellationToken)

非同步讀取輸入字串中的所有字元,從當前位置開始,並將當前位置推進到輸入字串的末尾。

ReadBlock(Char[], Int32, Int32)

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

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

讀取輸入字串中從當前位置開始的所有字元,並將當前位置推進到輸入字串的末尾。

ReadBlockAsync(Char[], Int32, Int32)

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

ReadBlockAsync(Memory<Char>, CancellationToken)

非同步讀取輸入字串中從當前位置開始的所有字元,並將當前位置推進到輸入字串的末尾。

ReadLine()

從目前字串讀取一行字元,並以字串形式回傳資料。

ReadLineAsync()

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

ReadToEnd()

讀取從當前位置到字串末尾的所有字元,並以單一字串形式返回。

ReadToEndAsync()

非同步讀取從當前位置到字串末尾的所有字元,並以單一字串回傳。

ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
IDisposable.Dispose()

關於此成員的描述,請參見 Dispose()

(繼承來源 TextReader)

適用於

另請參閱