TextFieldParser.PeekChars(Int32) 方法

定義

讀取指定字元數且不推進游標。

public:
 System::String ^ PeekChars(int numberOfChars);
public string PeekChars(int numberOfChars);
member this.PeekChars : int -> string
Public Function PeekChars (numberOfChars As Integer) As String

參數

numberOfChars
Int32

字數的數量。 必須的。

傳回

包含指定字元數的字串。

例外狀況

numberOfChars 小於0。

範例

這個範例用 PeekChars 來找到資料的結尾,並在該點停止解析檔案。

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {","}
    MyReader.CommentTokens = New String() {"'"}
    Dim currentRow As String()
    While (MyReader.PeekChars(1) IsNot "")
        Try
            currentRow = MyReader.ReadFields()
            For Each currentField As String In currentRow
                My.Computer.FileSystem.WriteAllText(
                   "C://testfile.txt", currentField, True)
            Next
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try
    End While
End Using

備註

numberOfChars 值必須小於該行中字元總數。 若不符合,回傳的 PeekChars 字串將被截斷至該行長度。

空白行會遭到忽略。

行尾字元則不回傳。

PeekChars 方法不進行解析;在分隔欄位內的行尾字元會被解讀為該行的實際結尾。

下表列出與此 PeekChars 方法相關的任務範例。

看!
在解析前先確定欄位的格式 如何:從具有多種格式的文本文件讀取

適用於

另請參閱