Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

counting occurrences of end of line

Tags:

string

c#

linq

I have some string with text, I want to count all occurrences of Environment.NewLine.

I thought to something in a way like

MyString.Where(c => c == Environment.NewLine).Count();

But c is only one char so it will not work .

Any better suggestions ?

like image 661
Night Walker Avatar asked Jul 02 '11 15:07

Night Walker


1 Answers

With Regex:

int count = Regex.Matches(input, Environment.NewLine).Count;

With String.Split:

int count = input.Split(new string[] { Environment.NewLine },
                      StringSplitOptions.None).Length - 1;
like image 185
Ahmad Mageed Avatar answered Sep 19 '22 23:09

Ahmad Mageed