Export (0) Print
Expand All

TextFieldParser.PeekChars Method

Reads the specified number of characters without advancing the cursor.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public string PeekChars(
	int numberOfChars
)

Parameters

numberOfChars
Type: System.Int32
Int32. Number of characters to read. Required.

Return Value

Type: System.String
A string that contains the specified number of characters read.

ExceptionCondition
ArgumentException

numberOfChars is less than 0.

The numberOfChars value must be less than the total number of characters in the line. If it is not, the string returned by PeekChars will be truncated to the length of the line.

Blank lines are ignored.

End-of-line characters are not returned.

The PeekChars method performs no parsing; an end-of- line character within a delimited field is interpreted as the actual end of the line.

The following table lists examples of tasks involving the PeekChars method.

To

See

Determine the format of a field before parsing it

How to: Read From Text Files with Multiple Formats in Visual Basic

This example uses PeekChars to find the end of the data and stop parsing the file at that point.


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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft