Getting a linecount from a text document?

  • Thread starter Thread starter Sandman
  • Start date Start date
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.
 
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
 
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.
.
 
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.
 
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++;
}
 
Back
Top