Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

Counting lines in a string

, 17 Jan 2012
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

Comments and Discussions

 
GeneralRe: Damn! :-) Thanks for trying it! PinmemberJon Bellamy18-Jan-12 12:32 
GeneralRe: :doh: I forgot you could rewind the underlying stream! :O Mi... PinmvpOriginalGriff17-Jan-12 9:33 
GeneralAddendum: Removing the Dispose brought the time down by 10 m... PinmvpOriginalGriff17-Jan-12 8:08 
GeneralRe: Thanks for trying, could you at least humour the following t... PinmemberJon Bellamy17-Jan-12 9:04 
GeneralNot only is this about strings rather than files, but it is ... PinmvpOriginalGriff17-Jan-12 8:07 
General@Jon - Your code is for counting lines in a FILE. This artic... Pinmembertmbgfan17-Jan-12 6:05 
GeneralRe: Thank you for pointing that out - with capitals. Whilst I u... PinmemberJon 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 | Mobile
Web01 | 2.8.140926.1 | Last Updated 17 Jan 2012
Article Copyright 2012 by Jon Bellamy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid