Redigera

Dela via


TextFieldParser Class

Definition

Provides methods and properties for parsing structured text files.

public ref class TextFieldParser : IDisposable
public class TextFieldParser : IDisposable
type TextFieldParser = class
    interface IDisposable
Public Class TextFieldParser
Implements IDisposable
Inheritance
TextFieldParser
Implements

Examples

This example parses through a tab-delimited text file, Bigfile.

Using MyReader As New Microsoft.VisualBasic.FileIO.
    TextFieldParser("c:\logs\bigfile")

    MyReader.TextFieldType = 
        Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {vbTab}
    Dim currentRow As String()
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row.
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & 
            " is invalid.  Skipping")
        End Try
    End While
End Using

Remarks

The TextFieldParser object provides methods and properties for parsing structured text files. Parsing a text file with the TextFieldParser is similar to iterating over a text file, while using the ReadFields method to extract fields of text is similar to splitting the strings.

The TextFieldParser can parse two types of files: delimited or fixed-width. Some properties, such as Delimiters and HasFieldsEnclosedInQuotes are meaningful only when working with delimited files, while the FieldWidths property is meaningful only when working with fixed-width files.

The following table lists examples of tasks involving the Microsoft.VisualBasic.FileIO.TextFieldParser object.

To See
Read from a delimited text file How to: Read From Comma-Delimited Text Files
Read from a fixed-width text file How to: Read From Fixed-width Text Files
Read from a text file with multiple formats How to: Read From Text Files with Multiple Formats

Constructors

TextFieldParser(Stream)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding, Boolean)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding, Boolean, Boolean)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(String)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(String, Encoding)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(String, Encoding, Boolean)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(TextReader)

Initializes a new instance of the TextFieldParser class.

Properties

CommentTokens

Defines comment tokens. A comment token is a string that, when placed at the beginning of a line, indicates that the line is a comment and should be ignored by the parser.

Delimiters

Defines the delimiters for a text file.

EndOfData

Returns True if there are no non-blank, non-comment lines between the current cursor position and the end of the file.

ErrorLine

Returns the line that caused the most recent MalformedLineException exception.

ErrorLineNumber

Returns the number of the line that caused the most recent MalformedLineException exception.

FieldWidths

Denotes the width of each column in the text file being parsed.

HasFieldsEnclosedInQuotes

Denotes whether fields are enclosed in quotation marks when a delimited file is being parsed.

LineNumber

Returns the current line number, or returns -1 if no more characters are available in the stream.

TextFieldType

Indicates whether the file to be parsed is delimited or fixed-width.

TrimWhiteSpace

Indicates whether leading and trailing white space should be trimmed from field values.

Methods

Close()

Closes the current TextFieldParser object.

Dispose()

Releases resources used by the TextFieldParser object.

Dispose(Boolean)

Releases resources used by the TextFieldParser object.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize()

Allows the TextFieldParser object to attempt to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
PeekChars(Int32)

Reads the specified number of characters without advancing the cursor.

ReadFields()

Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.

ReadLine()

Returns the current line as a string and advances the cursor to the next line.

ReadToEnd()

Reads the remainder of the text file and returns it as a string.

SetDelimiters(String[])

Sets the delimiters for the reader to the specified values, and sets the field type to Delimited.

SetFieldWidths(Int32[])

Sets the delimiters for the reader to the specified values.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IDisposable.Dispose()

Releases resources used by the TextFieldParser object.

Applies to

See also