Pseudo System Modal DialogRegular Expressions Tester

Using TextFieldParser

May 12th, 2007

TextFieldParser does the heavy lifting for youParsing a delimited string can be tricky. Sometimes there may be multiple delimiters, strings enclosed in quotes with the delimiters inside as well as other complications. The VB.NET TextFieldParser helps simply dealing with these kinds of strings easier.

The TextFieldParser does all the heavy lifting for you. All you really need to tell it are a few simple facts, like which delimiters to use, what to do about quotes and a few other options, and it does the rest.

The function example below is designed to do a split on a delimited string that contains strings in quotes that may or may not contain one of the delimiter characters. You pass in the delimited string and the delimiters and it returns the values in a generic collection List object. You could use a standard String array if you wanted but I’ve found the generic collection objects to be much better in a number of ways (yes, that will be in another article). In the example, I’m using an in-memory stream. However, you could easily take this code and expand it to using a FileStream instead. Also note the Using statements that help insure that memory is released when the work is done.

Public Function QuoteSplit(ByVal parseString As String, ByVal ParamArray Delimiters() As String) As List(Of String)
    Dim Results() As String
    Dim StringEncoding As New ASCIIEncoding()
    Using MemStream As New MemoryStream(StringEncoding.GetBytes(parseString))
        Using Parser As New FileIO.TextFieldParser(MemStream)
            Parser.Delimiters = Delimiters
            Parser.HasFieldsEnclosedInQuotes = True
            Results = Parser.ReadFields()
        End Using
    End Using
    Return New List(Of String)(Results)
End Function
Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: Code Examples


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (1 votes, average: 5 out of 5)
Loading ... Loading ...

1 Comment Add your own

  • 1. anujj  |  October 5th, 2009 at 4:44 am

    hello experts,
    if anybody can help me out with VB.net,
    How can i use the contents of a textbox, delimit it using 2 or more delimeters but one at a time, and display it in a datagridview (or may be a text box).
    a delimeter is chosen via a radiobutton which will immediately delimit and show the delimited contents.
    i just have started using vb.net, so any examples will help me..
    thanks in advance!

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives