counting occurrences of end of line
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 no开发者_如何学Got work .
Any better suggestions ?
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;
精彩评论