Click here to Skip to main content
11,495,486 members (68,755 online)
Click here to Skip to main content

Tagged as

Counting lines in a string

, 10 Jan 2012 CPOL 3.5K
Rate this:
Please Sign up or sign in to vote.
You shouldn't be working with huge strings at all.If this represents a file's content, read it using File.ReadAllLines() and take the array's Length.Otherwise, count the lines while you collect the data, not afterwards.Dealing with a huge string isn't doing the caches any favors.And if...
You shouldn't be working with huge strings at all.
If this represents a file's content, read it using File.ReadAllLines() and take the array's Length.
Otherwise, count the lines while you collect the data, not afterwards.

Dealing with a huge string isn't doing the caches any favors.
And if you can't avoid it, I would consider:
static long LinesCount(string s) {return s.Length-s.Replace("\n","").Length;}

which was in one of my very first CP posts, a couple of years ago.

Smile | :)

License

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

Share

About the Author

Luc Pattyn
Software Developer (Senior) Perceler
Belgium Belgium
I am an engineer with a background in electronics, software and mathematics.

I develop technical software, both for embedded systems and for desktop equipment. This includes operating systems, communication software, local networks, image processing, machine control, automation, etc.

I have been using all kinds of microcontrollers and microprocessors (Intel 4004/8080/8051/80386/Pentium, Motorola 680x/680x0/ColdFire/PowerPC, Microchip PIC, Altera NIOS, and many more), lots of programming languages (all relevant assemblers, Fortran, Basic, C, Java, C#, and many more), and different operating systems (both proprietary and commercial).

For desktop applications and general development tools I have been using both UNIX systems and Mac/MacOS for many years, but I have switched to x86-based PCs with Windows, Visual Studio and the .NET Framework several years ago.

I specialize in:
- cross-platform development (making software that runs on diverse hardware/OS combinations)
- instruction set simulation
- improving software performance, i.e. making sure the software runs the job at hand in as short a time as possible on the given hardware. This entails algorithm selection, implementation design, accurate measurements, code optimisation, and sometimes implementing virtual machines, applying SIMD technology (such as MMX/SSE), and more.

Comments and Discussions

 
GeneralActually I'm not surprised other alternatives are faster; it... Pin
Luc Pattyn10-Jan-12 22:34
mvpLuc Pattyn10-Jan-12 22:34 
GeneralRe: Accessing the Length property doesn't scan the string; the l... Pin
Richard Deeming24-Jan-12 6:51
memberRichard Deeming24-Jan-12 6:51 
GeneralI agree - I just load a large text file to give the timing c... Pin
OriginalGriff10-Jan-12 22:17
mvpOriginalGriff10-Jan-12 22:17 

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.150520.1 | Last Updated 10 Jan 2012
Article Copyright 2012 by Luc Pattyn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid