Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Counting lines in a string

Rate me:
Please Sign up or sign in to vote.
4.89/5 (9 votes)
9 Jan 2012CPOL 10.6K   2   3
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:


C#
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)


Written By
Technical Lead E.ON
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Nice bunch Pin
thatraja10-Jan-12 8:59
professionalthatraja10-Jan-12 8:59 
GeneralReason for my vote of 5 Great Work! Pin
Grasshopper.iics9-Jan-12 6:51
Grasshopper.iics9-Jan-12 6:51 
GeneralI wasn't trying to fine tune my solution to the fastest poss... Pin
OriginalGriff9-Jan-12 2:56
mveOriginalGriff9-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.