|
-
Mar 19th, 2010, 08:12 AM
#1
Thread Starter
New Member
delimited text file into array()
Hi guys, I have a text file with for example this data in it :
"19 Mar 2010","6:00","16:00","10","1","9","6.61","£59.49¬"
"20 Mar 2010","9:45","19:15","10","1","9","6.61","£69.89¬"
"21 Mar 2010","9:00","17:00","8","1","8","6.61","£99.32¬"
I used " , " to delimit individual pieces of data and " ¬ " to denote end of line.
I'd like to be able to split the files into lines and assign each line to an array then split each of those array strings into the comma " , " delimited data and feed each piece of data into another array ready for writing to a datagridview.
I've spent a while trying to get Split() to work but I get errors regarding converting strings to one-dimensional arrays ?
Also the Split() method asks for a "ParamArray" but I haven't been able to find examples of other people providing them on the net any idea what those are about?
Hope I've made sense ??
Thanks in advance....
-
Mar 19th, 2010, 09:05 AM
#2
Re: delimited text file into array()
Look at using a TextFieldParser. The documentation contains code examples.
-
Mar 19th, 2010, 09:13 AM
#3
Re: delimited text file into array()
Code:
Dim ts As String = "19 Mar 2010,6:00,16:00,10,1,9,6.61,£59.49¬20 Mar 2010,9:45,19:15,10,1,9,6.61,£69.89¬21 Mar 2010,9:00,17:00,8,1,8,6.61,£99.32¬"
Dim l2() As String = New String() {}
Dim splitCH() As Char = New Char() {","c, "¬"c}
l2 = ts.Split(splitCH)
-
Mar 19th, 2010, 10:24 AM
#4
Thread Starter
New Member
Re: delimited text file into array()
Thanks for your replys, textfieldparsers is just confusing me
All I want to do is read a line of a text file, split it into an array... do whatever with that array. Then, read the NEXT line of the text file, split that to an array ... do whatever with that array and so on until i reach the end of the text file.
Is there an answer as simple as I've described?
Code:
Dim ts As String = "19 Mar 2010,6:00,16:00,10,1,9,6.61,£59.49¬20 Mar 2010,9:45,19:15,10,1,9,6.61,£69.89¬21 Mar 2010,9:00,17:00,8,1,8,6.61,£99.32¬"
Dim l2() As String = New String() {}
Dim splitCH() As Char = New Char() {","c, "¬"c}
l2 = ts.Split(splitCH)
I'm also finding the above doesn't work for me because the variable "ts" isn't going to equal a literal string its going to be the contents of a text file I can only seem to get working with something like:
Code:
Dim returnValue As String()
returnValue = My.Computer.FileSystem.ReadAllText(FileName).Split("¬")
and when I try assigning ts = returnValue it throws an error?
I feel like I'm going mad here
-
Mar 19th, 2010, 10:36 AM
#5
Re: delimited text file into array()
 Originally Posted by James_C
Thanks for your replys, textfieldparsers is just confusing me
All I want to do is read a line of a text file, split it into an array
Code:
Dim ts As String = "19 Mar 2010,6:00,16:00,10,1,9,6.61,£59.49¬20 Mar 2010,9:45,19:15,10,1,9,6.61,£69.89¬21 Mar 2010,9:00,17:00,8,1,8,6.61,£99.32¬"
Dim l2() As String = New String() {}
Dim splitCH() As Char = New Char() {","c, "¬"c}
l2 = ts.Split(splitCH)
I'm also finding the above doesn't work for me because the variable "ts" isn't going to equal a literal string ....
What if you put that code in a function and passed the line just read to the function?
Code:
dim myStr() as string = foobar(line just read)
public function foobar (ts as string) as string()) '
Dim l2() As String = New String() {}
Dim splitCH() As Char = New Char() {","c, "¬"c}
l2 = ts.Split(splitCH)
return l2
end function
-
Mar 19th, 2010, 11:29 AM
#6
Thread Starter
New Member
Re: delimited text file into array()
Sorry dbasnett but I just don't understand how your code works which makes it difficult for me.
I'm not sure how I'm supposed to pass a line into the function considering I'm using .ReadAllText ?
Can you tell me how I read line by line through a text file please ?
-
Mar 19th, 2010, 01:47 PM
#7
Re: delimited text file into array()
Pass the whole string in.
-
Mar 19th, 2010, 01:50 PM
#8
Re: delimited text file into array()
Code:
'
Dim readText As String = IO.File.ReadAllText(path)
Dim myStr() As String = foobar(readText)
Public Function foobar(ByVal ts As String) As String() '
Dim l2() As String = New String() {}
Dim splitCH() As Char = New Char() {","c, "¬"c}
l2 = ts.Split(splitCH)
Return l2
End Function
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|