csv string.split

Ministry

Member
Joined
Apr 7, 2005
Messages
12
Programming Experience
1-3
I understand the string.split method extracts the sunstrings into an array but how do is the array index accessed?

or does anyone know of an easier way to extract the csv values?
 

Schenz

Well-known member
Joined
Sep 11, 2004
Messages
181
Location
Cincinnati, OH
Programming Experience
5-10
This Function assumes you want to return the values in the CSV file as a DataSet:

VB.NET:
Function delimitedDataSet(ByVal strDelimiter As String, ByVal strFilePath As String) As DataSet
		Dim oDS As New DataSet
		Dim strFields As String
		Dim oTable As New DataTable
		Dim oRows As DataRow
		Dim intCounter As Int32 = 0
		Dim oRow As DataRow()
		Dim cancel As Boolean

		oDS.DataSetName = "Dataset"
		oDS.Tables.Add("Orders")

		Dim oSR As New StreamReader(strFilePath)

		'Go to the top of the file
		oSR.BaseStream.Seek(0, SeekOrigin.Begin)

		'Add in the Header Columns
		For Each strFields In oSR.ReadLine().Split(strDelimiter.ToCharArray)
			Dim FieldName As String = Trim(strFields)
			oDS.Tables(0).Columns.Add(FieldName)
		Next

		'Now add in the Rows
		oTable = oDS.Tables(0)
		' Read each line in file
		While (oSR.Peek() > -1)
			' NewRow buffer
			oRows = oTable.NewRow()
			' Read each "Field" in the line
			For Each strFields In oSR.ReadLine().Split(strDelimiter.ToCharArray)
			    ' Check first field - if nothing then cancel reading the rest of the row
				If intCounter = 0 Then
				    If strFields <> "" Then
					    ' Read value of the field into the Field variable
					    Dim Field As String = Trim(strFields.Replace("""", ""))
					    ' add Field to the NewRow buffer
					    oRows(intCounter) = Field
					    ' increment field count
					    intCounter = intCounter + 1
					Else
					    ' nothing in field cancel the rest of the read
					    cancel = True
					    Exit For
					End If
				Else
				    ' Read value of the field into the Field variable
				    Dim Field As String = Trim(strFields)
				    ' add Field to the New Row Buffer
				    oRows(intCounter) = Field
				    ' increment field count
				    intCounter = intCounter + 1
				End If
			Next

			If cancel Then
				' reset the field counter
				intCounter = 0
			Else
				' reset the field counter
				intCounter = 0
				oTable.Rows.Add(oRows)
			End If
		End While

		oSR.Close()

		Return oDS
	End Function
 

dashley

Well-known member
Joined
May 27, 2005
Messages
59
Location
Tennessee
Programming Experience
10+
Ministry,

Heres a very easy way to do what you want. If you want do go deeper I can send you some code.

Dan

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] split()
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myCSV [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myArray() [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i

myCSV = "One , two , three, four , five"

myArray = myCSV.Split(",") [/size][size=2][color=#008000]' Delimeter[/color][/size]
 
[size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] 4
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1.Items.Add(myArray(i))

[/size][size=2][color=#0000ff]Next

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size]
 
Top Bottom