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

Counting lines in a string

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Jan 2012CPOL 8K   1
How about using the extension method: return s.Count(c => (c == '\n'));
How about using the extension method:
return s.Count(c => (c == '\n'));

License

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


Written By
Student
Wales Wales
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralYou do realise that that is Linq in disguise? :laugh: Index ... Pin
OriginalGriff10-Jan-12 8:37
mveOriginalGriff10-Jan-12 8:37 
You do realise that that is Linq in disguise? Laugh | :laugh:
Index (1) : 166
Index (2) : 955
Index (3) : 1024
Index (4) : 162
Index (5) : 1217
Index (6) : 1545
Index (7) : 1091
Split : 1080
Regex : 2516
Linq (Expl) : 3577
Linq Count : 3541
Second slowest!

I am going to have to update this tip to show all the methods so far - I was just waiting for Aho-Corasick to be implemented and see what it did...

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.