[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

CSV Parser - VERY Small Function!

David Kaye

9/3/2011 4:51:00 PM

I've been unsatisifed with the CSV parsers I've found, the kind that can
take
an input such as the following and create proper strings out of it:

Example: Superman,1234,"Kent, Clark",Kryptonite

A normal split function would create 5 strings instead of 4 because it would
see the
comma within the quotes as a delimiter, which it isn't.

Okay, here's my routine. I knew it had to be far simpler to write than the
gargantuan routines I've seen written elsewhere. So, for the betterment of
the Visual Basic artform, here's my routine. ParseCSV% returns the number
of fields, text$ is the input text, delimiter$ is the delimiter such as
Chr$(44), and output%() is the array of resulting strings. Enjoy.



Function ParseCSV%(text$, delimiter$, output$())
Rem parses fields where delimiters are within quotes
Rem such as "Kent, Clark" 9/2/2011 david kaye
ReDim output$(50) 'up to 50 fields or whatever you set
Dim i%, a$, fld%, flip As Boolean
fld% = 1
For i% = 1 To Len(text$)
a$ = Mid$(text$, i%, 1)
Select Case a$
Case Chr$(34)
flip = Not flip
Case delimiter$
If flip Then
output$(fld%) = output$(fld%) & delimiter$
Else
fld% = fld% + 1
End If
Case Else
output$(fld%) = output$(fld%) & a$
End Select
Next
ParseCSV% = fld%
End Function