by admin » Mon Mar 27, 2006 8:10 pm
This code willsplit command sperated text file into array.
- Code: Select all
Private Sub ParseCSV()
Dim oneLine As String = _
"""quoted"",nonquoted,""comma, """"inner-quoted-comma"""", chamelean"",,after-empty"
Console.Write("test : " & txtsrc.Text)
txtout.Text = txtsrc.Text & vbCrLf
Dim fieldValues As String()
If Len(Trim(txtsrc.Text)) > 3 Then
fieldValues = ParseLine(txtsrc.Text)
Else
fieldValues = ParseLine(oneLine)
End If
For i As Integer = 0 To fieldValues.Length - 1
txtout.Text = txtout.Text & i & " ==> " & fieldValues(i) & vbCrLf
Console.WriteLine(i & " [" & fieldValues(i) & "]")
Next
End Sub
Private Shared Function ParseLine(ByVal oneLine As String) As String()
' Returns an array containing the values of the comma-separated fields.
' This pattern actually recognizes the correct commas.
' The Regex.Split() command later gets text between the commas.
Dim pattern As String = ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
Dim r As System.Text.RegularExpressions.Regex = _
New System.Text.RegularExpressions.Regex(pattern)
Return r.Split(oneLine)
End Function