Click here to Skip to main content
11,634,622 members (66,503 online)
Click here to Skip to main content

Tagged as

Counting lines in a string

, 17 Jan 2012 CPOL 3.2K
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Jon Bellamy
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: Damn! :-) Thanks for trying it! Pin
Jon Bellamy18-Jan-12 12:32
memberJon Bellamy18-Jan-12 12:32 
GeneralRe: :doh: I forgot you could rewind the underlying stream! :O Mi... Pin
OriginalGriff17-Jan-12 9:33
mvpOriginalGriff17-Jan-12 9:33 
GeneralAddendum: Removing the Dispose brought the time down by 10 m... Pin
OriginalGriff17-Jan-12 8:08
mvpOriginalGriff17-Jan-12 8:08 
GeneralRe: Thanks for trying, could you at least humour the following t... Pin
Jon Bellamy17-Jan-12 9:04
memberJon Bellamy17-Jan-12 9:04 
GeneralNot only is this about strings rather than files, but it is ... Pin
OriginalGriff17-Jan-12 8:07
mvpOriginalGriff17-Jan-12 8:07 
General@Jon - Your code is for counting lines in a FILE. This artic... Pin
tmbgfan17-Jan-12 6:05
membertmbgfan17-Jan-12 6:05 
GeneralRe: Thank you for pointing that out - with capitals. Whilst I u... Pin
Jon Bellamy17-Jan-12 6:25
memberJon Bellamy17-Jan-12 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 17 Jan 2012
Article Copyright 2012 by Jon Bellamy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid