Discussion on:

4
Comments

Join the conversation!

Follow via:
RSS
Email Alert
Absent from VB6, and apparently still absent from .NET is an easy method to parse real-world CSV (comma-separated-value) files.

Here's a typical CSV string...

14325,George Smith,123 Elm St,"Dayton, OH 45420",A,3

...and you want it split like this...

14325
George Smith
123 Elm St
Dayton, OH 45420
A
3

...where the comma in the quoted string is DATA, not a separator, and the quotes are NOT data.

It seems one still must roll their own routine, buy a 3rd-party component (if you can find one), or in .NET write a moderately complex regular expression.

The routines have been in various system software for years - they just don't seem to ever give us a handle to them for our own use.

In the old 1980's BASIC dialects, such a comma-separated string could be in a DATA statement and READ statements would get the pieces.

Oh well - I guess I'll keep using my home-grown code for CSV strings.

::sigh::
The addition of generics in .NET 2.0 have made consolidating this task a much easier proposition. See an example here: Parsing Delimited Strings Using Generics
0 Votes
+ -
Thanks

I wrote this code after I sow your Example. I hop it's useful for you and for the users.

The follows code for VB.NET

Public Function Gettoks(ByVal GettokText As String, ByVal GettokStr As String) As Integer
Gettoks = GettokText.Split(GettokStr).Length
End Function
Public Function Gettok(ByVal GettokText As String, ByVal GettokStr As String, ByVal GettokNum As Integer) As String
Dim GettokTmp As String() = Nothing
GettokNum = GettokNum - 1
GettokTmp = GettokText.Split(GettokStr)
Try
Gettok = GettokTmp(GettokNum)
Catch ex As Exception
Gettok = Nothing
End Try
End Function
Private Sub CmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdTest.Click
Dim values As String
values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com"
MsgBox("Gettoks Value is: " & Gettoks(values, ",")) 'Return 5
MsgBox("Value Numper '3' is: " & Gettok(values, ",", 3)) 'Return News.com
End Sub
I often like to assign the individual fields from a delimited string into their own variables. This is easily done in perl like this (algorithmically):
if the string $A is "one/two/three", I can assign three variables in one step like this:
(first, second, third) = split($A,"/")
Is there any way this can be accomplished similarly in VB.net?
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.