Results 1 to 3 of 3

Thread: csv string.split

  1. #1
    Ministry is offline VB.NET Forum Newbie
    .NET Framework
    Join Date
    Apr 2005
    Posts
    12
    Reputation
    102

    csv string.split

    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?

  2. #2
    Schenz is offline VB.NET Forum Genius
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2004
    Location
    Cincinnati, OH
    Posts
    181
    Reputation
    112
    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

  3. #3
    dashley is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    May 2005
    Location
    Tennessee
    Posts
    59
    Reputation
    102
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking