StringReader 類別

定義

實作讀取字串的 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讀取一個字元,一次使用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 類別的新執行個體,這個執行個體會從指定的字串讀取。Initializes a new instance of the StringReader class that reads from the specified string.

方法

Close()

關閉 StringReaderCloses the StringReader.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 TextReader 物件使用的所有資源。Releases all resources used by the TextReader object.

(繼承來源 TextReader)
Dispose(Boolean)

釋放 StringReader 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the StringReader and optionally releases the managed resources.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
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)

自輸入字串讀取字元區塊,並將字元位置前移 count 個字元。Reads a block of characters from the input string and advances the character position by count.

Read(Span<Char>)
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)
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.

(繼承來源 TextReader)
ReadBlock(Span<Char>)
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)
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.

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.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

如需這個成員的說明,請參閱 Dispose()For a description of this member, see Dispose().

(繼承來源 TextReader)

適用於

另請參閱