Getting a linecount from a text document?

S

Sandman

Hi,

This is probably an ultra easy one. But I am using streamreader to
read the contents of a text document into memory. I was wondering how
one would go about getting a linecount from a text document?

Any ideas on how to do this in VB.NET?
thanks,
B.
 
A

Alex Yakhnin [eMVP]

You can "split" the resulting text by using a CR LF as a
separator, i.e.:

string[] lines = resultString.Split('\n\r');
int lineCount = lines.Length;

-Alex
 
N

Neil Cowburn [MVP]

Replace '\n\r' in the Split with '\r\n'


Alex Yakhnin said:
You can "split" the resulting text by using a CR LF as a
separator, i.e.:

string[] lines = resultString.Split('\n\r');
int lineCount = lines.Length;

-Alex
-----Original Message-----
Hi,

This is probably an ultra easy one. But I am using streamreader to
read the contents of a text document into memory. I was wondering how
one would go about getting a linecount from a text document?

Any ideas on how to do this in VB.NET?
thanks,
B.
.
 
S

Sandman

Alex Feinman said:
Keep calling ReadLine() and incrementing a counter

Thanks Alex,

I tried doing that, but different devices will have different numbers
of lines in their text files. If there whas a way to readline() until
end or something that would be perfect, but I haven't found it yet.

Feeling really dumb right now :(

B.
 
A

Alex Feinman [MVP]

That's what I mean - keep calling ReadLine until it returns null (or Nothing
in VB). That means you've hit the end of file:
string s;
StreamReader rdr;
int count = 0;
while(true)
{
s = rdr.ReadLine();
if ( s == null )
break;
count++;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top