0

I am reading data in from a text file. I know that each piece of data is separated by a comma and it looks like this in the text file:

 "Requisition","Supplies Req GL.pdf","05/28/2014","8,200.00","0510","86107RC"

Here's where I am drawing a blank. The 4th piece of data on the line can contain commas, so when I do a split on the data, it also splits that piece as well.

How can I read this in, separate the data and also keep column 4 in tact.

4
  • 4
    Look into the TextFieldParser Commented May 28, 2015 at 18:28
  • As an alternative, there is an overload of String.Split which accepts multiple character strings as delimiters (ie: ","). Commented May 28, 2015 at 18:50
  • Then the way the file was built is flawed. I would have removed the quotes also. Commented May 28, 2015 at 19:19
  • TextFieldParser worked like a charm ... thanks Plutonix Commented May 28, 2015 at 20:00

1 Answer 1

1

If you know it's that field, then just join them together... a little hokey but it works:

    Dim inLine As String()
    Dim columns As New List(Of String)
    Using sr as As New IO.StreamReader(args(0))
        While Not sr.EndOfStream
            inLine = sr.ReadLine.Trim().Split(CChar(","))
            columns.Add(inLine(0))
            columns.Add(inLine(1))
            columns.Add(inLine(2))
            If inLine.Length > 6 Then
                columns.Add(inLine(3) & inLine(4))
                columns.Add(inLine(5))
                columns.Add(inLine(6))
            Else
                columns.Add(inLine(3))
                columns.Add(inLine(4))
                columns.Add(inLine(5))
            End If
        End While
    End Using
Sign up to request clarification or add additional context in comments.

1 Comment

why not use the Using blocks for the StreamReader?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.