Results 1 to 3 of 3

Thread: [RESOLVED] Read plain text delimited by fieldnames

  1. #1

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Resolved [RESOLVED] Read plain text delimited by fieldnames

    Hi,

    for example I have this plain text:

    ID=100000CUSTOMER=JohnAMOUNT=2300020INVOICES=123CALLS=12ID=100001CUSTOMER=MaryAMOUNT=589INVOICES=2CA LLS=0ID=1002122CUSTOMER=DanielAMOUNT=8900000INVOICES=5CALLS=1
    etc etc

    Where "ID", "CUSTOMER", "AMOUNT", "INVOICES", "CALLS" are fields and I need get its values.

    is there any way, most automatic possible, to recover those values? If they were delimited by fixed delimiter, I could do it with split......


    thanks in advance

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Read plain text delimited by fieldnames

    You can try something like this:
    Code:
    'Create a datatable to hold the records
            Dim myTable As New DataTable()
            With myTable.Columns
                .Add("ID", GetType(Integer))
                .Add("CUSTOMER", GetType(String))
                .Add("AMOUNT", GetType(Decimal))
                .Add("INVOICES", GetType(Integer))
                .Add("CALLS", GetType(Integer))
            End With
            'Start parsing the text into records and populate the datatable
            Dim txt As String = "ID=100000CUSTOMER=JohnAMOUNT=2300020INVOICES=123CALLS=12ID=100001CUSTOMER=MaryAMOUNT=589INVOICES=2CA LLS=0ID=1002122CUSTOMER=DanielAMOUNT=8900000INVOICES=5CALLS=1"
            Dim delimiters() As String = {"ID="}
            Dim records() As String = txt.Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries)
            For Each record As String In records
                Dim fields() As String = record.Split("=".ToCharArray)
                If fields.Length = 5 Then
                    Dim row As DataRow = myTable.NewRow()
                    row("ID") = CInt(fields(0).Replace("CUSTOMER", ""))
                    row("CUSTOMER") = fields(1).Replace("AMOUNT", "")
                    row("AMOUNT") = CDec(fields(2).Replace("INVOICES", ""))
                    row("INVOICES") = CInt(fields(3).Replace("CALLS", ""))
                    row("CALLS") = CInt(fields(4))
                    myTable.Rows.Add(row)
                End If
            Next
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: Read plain text delimited by fieldnames

    stanav, thank you very much. Perfect example :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width