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?
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?
This Function assumes you want to return the values in the CSV file as a DataSet:
Code: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
Brandon Schenz
Ministry,
Heres a very easy way to do what you want. If you want do go deeper I can send you some code.
Dan
Code:PrivateSub split() Dim myCSV AsString Dim myArray() AsString Dim i myCSV = "One , two , three, four , five" myArray = myCSV.Split(",") ' Delimeter For i = 0 To 4 Me.ListBox1.Items.Add(myArray(i)) Next EndSub
Dashley
Bookmarks