Count the end of line entry

I have a line with text, I want to count all occurrences of Environment.NewLine .

I thought of something like

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

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

Any best deals?

+6
source share
1 answer

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; 
+10
source

Source: https://habr.com/ru/post/891878/


All Articles