quick question about .split

spartan086

Member
Joined
Mar 23, 2011
Messages
9
Programming Experience
Beginner
i wrote a program for my company and it imports a .csv file i then split it by the ,. My problem that i now see at the end of the project is that 98 out of 1400 items have , in the descriptions that im seperating. This creates a problem when search for items. My question is is there a way to use more then just one char to split it? i was thinking of spliting using ",".
 
You can split using ",", but the CSV format only has quotes around the strings that have commas, so then you would have an issue with only retrieving half of the strings. A quick Google search returned this function:

VB.NET:
Function splitCSV(ByVal str As String) As String()
Dim ret(0) As String
Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(str)
    Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    Reader.SetDelimiters(","c)
    Dim currentRow As String()
    While Not Reader.EndOfData
       Try
           currentRow = Reader.ReadFields()
           Dim currentField As String
           For Each currentField In currentRow
               ReDim Preserve ret(ret.Length)
               ret(ret.Length - 1) = currentField
           Next
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
        End Try
    End While
End Using
Return ret
End Function

Hope this helps
-Josh
 
This output may be useful as well:

VB.NET:
        Dim str As String = """1"",""2"",""3"""
        MsgBox(str)
        For Each str2 As String In str.Split(""",""")
            MsgBox(str2)
        Next
        For Each str2 As String In Split(str, """,""")
            MsgBox(str2)
        Next

See how .Split acts differently than Split when passing a string value. Seems like .Split accepts character arrays, not strings, and the Split function will accept a string like you are looking for.
 
I've heard ADO.Net has the ability to correctly parse csv files & you get a nice data table as a result which means you can use a GridView's filter property to look at chunks of data from the big table.
 
JuggolaBrotha said:
I've heard ADO.Net has the ability to correctly parse csv files & you get a nice data table as a result which means you can use a GridView's filter property to look at chunks of data from the big table.

Textfile Connection String Samples - ConnectionStrings.com

Using the OleDb connections and the above link, which got me a connection string seems like that is the ideal way to work with CSV files next time I work with a CSV file I will for sure try using ADO.NET to do it. It would also be nice to see how Schema.ini files work that seems like some pretty cool stuff as well.
 
Private Sub inventoryLoad(ByVal inventory As String)
fileReader = My.Computer.FileSystem.ReadAllText(inventory)
For Each str2 As String In Split(fileReader, ",")

Next

End Sub

so i tried plugging in what you said, but the program freezes. So i tried removing everything down to just loading the basics and it still just sits there, can you see what im doing wrong? inventory is the .csv file with 14000 items in it.
 
That is quite a few items, try using a CSV file with less items for testing.
 
ok it does work like above, it just takes a minute or two on this slow computer to load, which is fine ill just throw a splash screen up. But my new problem(thanks for sticking this out with me) is im loading everything into an array, so i make a string array called items and i added this code
VB.NET:
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] str2 [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Split(fileReader, tempString)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Preserve[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] items(x + 1)
items(x) = str2
x += 1
[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT]
redim is causing it to go into a infinite loop, if i take redim out and just set the array to items(5000000) it works.
 
Back
Top