Reputation: 497
I'm learning to read text files and Streamreader
is the only class I know until now for that purpose.
Until now I've seen this class lets me read the entire file(.ReadToEnd
), line by line(.ReadLine
) or character by character(.Read
).
However I can't figure how to read substrings delimited by ,
and solve this:
( Following 4 lines are the content of textfile.txt)
COLUMN1,MAIL1,COLUMN3,COLUMN4,COLUMN5,MAIL2
".","[email protected]","1","2013-05-08 00:00:00","0","[email protected]"
".","[email protected]","1","2013-05-08 00:00:00","0","[email protected]"
".","[email protected]","1","2013-05-08 00:00:00","0","[email protected]"
If the first email address is equal to the second, do:
Numberofmatched=Numberofmatched+1
If not
Numberofunmatched=Numberofunmatched+1
Take in consideration domain name can change and email addresses have variable lenght.
Any help?
Upvotes: 0
Views: 553
Reputation: 460158
Here's another method to read a text-file. You can use the System.IO.File
class, for example File.ReadLines
/File.ReadAllLines
or File.ReadAllText
.
Use String.Split
to get a String()
, one string for each column. Since the delimiter seems to be ,
instead of "
use String.Split(","c)
.
You can use following LINQ query which can increase readability:
Dim allLines = File.ReadAllLines("Path")
Dim data = From line In allLines.Skip(1) ' skip the header-line
Where Not String.IsNullOrWhiteSpace(line)
Let fields = line.Split(","c)
Where fields.Length = 6
Select fields
Dim Numberofmatched As Int32 = data.Count(Function(fields) fields(1) = fields(5))
Dim Numberofunmatched As Int32 = data.Count(Function(fields) fields(1) <> fields(5))
Note that you should use an available CSV-parser instead of reinventing the wheel since they support quoting characters and a lot of other things. One recommendable in the Visual-Basic namespace is the TextFieldParser
class.
Upvotes: 1