Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Counting lines in a string

5.00/5 (2 votes)
17 Jan 2012CPOL 10.3K  
Again, going with other people's comments about memory, how about using a Streamreader? (I haven't tested the timings (or code) but from memory..This should be fast (and memory efficient)...at least in my experience.StreamReader sr = new StreamReader(Put file here);Int32 cnt =...

Again, going with other people's comments about memory, how about using a Streamreader? (I haven't tested the timings (or code) but from memory..


This should be fast (and memory efficient)...at least in my experience.


C#
StreamReader sr = new StreamReader("Put file here");
Int32 cnt = 0;

while (sr.ReadLine() != null)
{
    cnt++;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)