65.9K
CodeProject is changing. Read more.
Home

Counting lines in a string

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (9 votes)

Jan 9, 2012

CPOL
viewsIcon

11524

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! } ...