StringReader.Read 方法

定義

自輸入字串讀取下一個字元或下一組字元。Reads the next character or next set of characters from the input string.

多載

Read()

自輸入字串讀取下一個字元,並將字元位置前移一個字元。Reads the next character from the input string and advances the character position by one character.

Read(Span<Char>)

從目前位置開始,讀取輸入字串中的所有字元,並將目前位置往前移至輸入字串的結尾。Reads all the characters from the input string, starting at the current position, and advances the current position to the end of the input string.

Read(Char[], Int32, Int32)

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

Read()

自輸入字串讀取下一個字元,並將字元位置前移一個字元。Reads the next character from the input string and advances the character position by one character.

public:
 override int Read();
public override int Read ();
override this.Read : unit -> int
Public Overrides Function Read () As Integer

傳回

基礎字串的下一個字元;如果不再有字元可供使用,則為 -1。The next character from the underlying string, or -1 if no more characters are available.

例外狀況

目前的讀取器已關閉。The current reader is closed.

範例

這個程式碼範例是針對 StringReader 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the StringReader class.

convertedCharacter = Convert::ToChar( intCharacter );
if ( convertedCharacter == '.' )
{
   strWriter->Write(  ".\n\n" );
   
   // Bypass the spaces between sentences.
   strReader->Read();
   strReader->Read();
}
convertedCharacter = Convert.ToChar(intCharacter);
if(convertedCharacter == '.')
{
    strWriter.Write(".\n\n");

    // Bypass the spaces between sentences.
    strReader.Read();
    strReader.Read();
}
convertedCharacter = Convert.ToChar(intCharacter)
If convertedCharacter = "."C Then
    strWriter.Write("." & vbCrLf & vbCrLf)

    ' Bypass the spaces between sentences.
    strReader.Read()
    strReader.Read()

備註

這個方法會覆寫 TextReader.Read 方法。This method overrides the TextReader.Read method.

下表列出其他一般或相關 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

另請參閱

Read(Span<Char>)

從目前位置開始,讀取輸入字串中的所有字元,並將目前位置往前移至輸入字串的結尾。Reads all the characters from the input string, starting at the current position, and advances the current position to the end of the input string.

public:
 override int Read(Span<char> buffer);
public override int Read (Span<char> buffer);
override this.Read : Span<char> -> int
Public Overrides Function Read (buffer As Span(Of Char)) As Integer

參數

buffer
Span<Char>

當這個方法傳回時,會包含讀取自目前來源的字元。When this method returns, contains the characters read from the current source. 如果讀取的字元總數為零,則範圍會保持不變。If the total number of characters read is zero, the span remains unmodified.

傳回

讀入緩衝區的字元總數。The total number of characters read into the buffer.

例外狀況

目前的字串讀取器執行個體已關閉。The current string reader instance is closed.

Read(Char[], Int32, Int32)

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

public:
 override int Read(cli::array <char> ^ buffer, int index, int count);
public override int Read (char[] buffer, int index, int count);
override this.Read : char[] * int * int -> int
Public Overrides Function Read (buffer As Char(), index As Integer, count As Integer) As Integer

參數

buffer
Char[]

當這個方法傳回時,會包含指定的字元陣列,這個陣列具有介於 index 到 (index + count - 1) 之間的值,已由讀取自目前來源的字元所取代。When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index
Int32

緩衝區中的起始索引。The starting index in the buffer.

count
Int32

要讀取的字元數。The number of characters to read.

傳回

讀入緩衝區的字元總數。The total number of characters read into the buffer. 如果目前無法提供那麼多的字元數,則這個數目可能小於所要求的字元數,或如果已經到達基礎字串的末端,則為零。This can be less than the number of characters requested if that many characters are not currently available, or zero if the end of the underlying string has been reached.

例外狀況

buffernullbuffer is null.

緩衝區長度減去 index 會小於 countThe buffer length minus index is less than count.

indexcount 是負數。index or count is negative.

目前的讀取器已關閉。The current reader is closed.

備註

這個方法會覆寫 TextReader.ReadThis method overrides TextReader.Read.

方法會從 StringReader 讀取 count 個字元到 buffer 的字元陣列,從位置 index開始。The method will read up to count characters from the StringReader into the buffer character array starting at position index. 傳回讀取的實際字元數,如果已到達字串結尾且未讀取任何字元,則傳回零。Returns the actual number of characters read, or zero if the end of the string has been reached and no characters are read.

下表列出其他一般或相關 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

另請參閱

適用於