Click here to Skip to main content
12,501,406 members (48,567 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.6K views
1 bookmarked
Posted

Counting lines in a string

, 9 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
I've compared your favorite with seven alternatives:static long LinesCount(string s) { long count = 0; int position = 0; while ((position = s.IndexOf('\n', position)) != -1) { count++; position++; // Skip this occurance! } ...

I've compared your favorite with seven alternatives:

static long LinesCount(string s)
 {
     long count = 0;
     int position = 0;
     while ((position = s.IndexOf('\n', position)) != -1)
     {
         count++;
         position++;         // Skip this occurance!
     }
     return count;
}
 
static long LinesCount2(string s)
{
     long count = 0;
     int posMax = s.Length;
 
     for (int position = 0; position < posMax; )
         if (s[position++] == '\n')
             count++;
 
     return count;
}
 
static long LinesCount3(string s)
{
     long count = 0;
     int posMax = s.Length;
     char[] a = s.ToCharArray();
 
     for (int position = 0; position < posMax; )
         if (a[position++] == '\n')
             count++;
 
     return count;
}
 
static long LinesCount4(string s)
{
     long count = 0;
     int position = -1;
     while ((position = s.IndexOf('\n', position + 1)) != -1)
     {
         count++;
     }
     return count;
}
 
static long LinesCount5(string s)
{
     long count = 0;
     int posMax = s.Length;
     char[] a = s.ToCharArray();
 
     foreach (char c in a)
         if (c == '\n')
             count++;
 
     return count;
}
 
static long LinesCount6(string s)
{
     long count = 0;
 
     foreach (char c in s)
         if (c == '\n')
             count++;
 
     return count;
}
 
static long LinesCount7(string s)
{
     return s.Length - s.Replace("\n", "").Length;
}
 
static long LinesCount8(string s)
{
     return s.Split('\n').Length - 1;
}

The fastest was LinesCount4() which is a slight variation of your original. I've just reduced the loop by one variable assignment. Some of the other contenders look nice. Others eat up a lot of memory. LinesCount6() is probably a good compromise between speed and style.

Greetings!

License

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

Share

About the Author

akemper
Technical Lead E.ON
Germany Germany
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralReason for my vote of 5 Nice bunch Pin
thatraja10-Jan-12 8:59
mvpthatraja10-Jan-12 8:59 
GeneralReason for my vote of 5 Great Work! Pin
Grasshopper.iics9-Jan-12 6:51
groupGrasshopper.iics9-Jan-12 6:51 
GeneralI wasn't trying to fine tune my solution to the fastest poss... Pin
OriginalGriff9-Jan-12 2:56
mvpOriginalGriff9-Jan-12 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.160919.1 | Last Updated 9 Jan 2012
Article Copyright 2012 by akemper
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid