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

Tagged as

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

Comments and Discussions

 
GeneralReason for my vote of 5 Nice bunch Pinmvpthatraja10-Jan-12 9:59 
GeneralReason for my vote of 5 Great Work! PingroupGrasshopper.iics9-Jan-12 7:51 
GeneralI wasn't trying to fine tune my solution to the fastest poss... PinmvpOriginalGriff9-Jan-12 3:56 

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.141223.1 | Last Updated 9 Jan 2012
Article Copyright 2012 by akemper
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid