Arrays & comma seperated text files

  • Thread starter Thread starter Danno
  • Start date Start date
D

Danno

Guest
I understand how to access a file with the StreamReader, but only with ReadLine(). How do I seperate that line into the three separate sections that are seperated by commas?
 
You could use the Split method of the string to split it into an array.

Code:
Dim sToSplit As String  String to split up
Dim sParts() As String  Comma-separated parts of the string

 Get sToSplit here by reading a line from the stream

sParts = sToSplit.Split(",")

The array item sParts(0) will be the first part, sParts(1) the
second, and sParts(2) the third.
 
Ok, great answer...but...

I get the following error with Option Strict on

"Option Strict On disallows implicit conversions from String to Char"

Here is my code (error area in bold):

Code:
Dim name(),  tempString As String

tempString = objSR.ReadLine
name = tempString.Split(",")
lstDetails.Items.Add(name)


Any suggestions? I tried to cast one way or the other with CChar but it hasnt worked.
 
Try the following.
Code:
Dim strName() As String, tempString As String, i As Integer

tempString = "Testing,out,explicit,type,casting"
strName = tempString.Split(","c)

For i = strName.GetLowerBound(0) To strName.GetUpperBound(0)
     lstDetails.Items.Add(strName(i))
Next
 
That give each item (thanks!) but I need to put each item into another array (three sections into 3 different arrays.....this is very confusing!

I need to break it up so that:

array1 = substring1
array2 = substring 2
array3 = substring 3

and do it in a loop (some way to modify your for/next loop?
 
And what is the easiest way to strip off the double quotes with Option Strict is on? The file has the first set of data in double quotes and if I try to Trim it is says it cant handle the double quote char within a string!
 
Back
Top