CSV file formats are actually quite complicated once you start handling all the special cases.
Why not use one already written: http://www.filehelpers.com
Main Topics
Browse All TopicsHello experts;
I am reading a CSV file one line at a time and splitting the string into an array. Some of the CSV fields are strings, others are numbers. The CustomerName field, for example, is a string that may contain commas. Is there an efficient way to get the Split function to ignore the delimiter if it is within quotes?
Any help or suggestions would be greatly appreciated.
Thank you in advance
PS I am coding in Vb.NET 2005
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
CSV file formats are actually quite complicated once you start handling all the special cases.
Why not use one already written: http://www.filehelpers.com
This isn't a solution, but it does relate to the issue that you are dealing with.
http://www.xbeat.net/vbspe
Here is an example of how one would parse using regex:
All you need is to change my regex function for one that works for your pattern:
Function SelectParser(ByVal SelectString As String) As ArrayList
Dim arSelect As New ArrayList
Dim s As String = SelectString
Dim mc As System.Text.RegularExpress
Dim result(mc.Count) As String
For i As Integer = 0 To mc.Count - 1
result(i) = mc(i).Value
Debug.WriteLine(String.For
arSelect.Add(mc(i).Value)
Next
Return arSelect
End Function
IF you need help with regex..here is a good link:
http://www.regular-express
Here's a comment on the regular expression approach: http://secretgeek.net/csv_
See if you agree.
If you have VB.Net 2005 (or above), then use the TextFieldParser() class:
Dim tfp As New Microsoft.VisualBasic.File
tfp.TextFieldType = FileIO.FieldType.Delimited
tfp.SetDelimiters(",")
tfp.HasFieldsEnclosedInQuo
tfp.TrimWhiteSpace = True
Dim fields() As String
While Not tfp.EndOfData
fields = tfp.ReadFields()
' ...do something with "fields"...
End While
tfp.Close()
if commas are the only problem..why not reformat the csv file so the delimiter is using a dash or / or any other symbol of your choice...
Then commas are of no issue.
No need to load proprietary software on a computer to handle splitting a string.
I could go on and on about proprietary software...what if one day they stop supporting it...or what if you change versions in your developing platform and they dont...what if they get hit by a car...
this is an easy vb.net solution....I say go for it -----cheers
Hease a simple way:
Dim tempLine As String
Dim seperatorChar As Char = "|"
tempLine = readLine.Replace(""",""", seperatorChar)
tempLine = tempLine.Replace("""", "")
splitter = tempLine.Split(seperatorCh
This works great _except_ when one of the fields has only a comma (,). Some CSV files also have a space following the comma-deliteter. Adjust the first line of Replace code as necessary.
I also have another way I'll offer in another comment.
Business Accounts
Answer for Membership
by: robbhillPosted on 2008-05-07 at 10:50:57ID: 21518745
yes..you will need to use a regex function I believe. There probably is another way...but regex might be the easiest.