开发者

counting occurrences of end of line

开发者 https://www.devze.com 2023-03-17 02:00 出处:网络
I have some string with text, I want to count all occurrences of Environment.NewLine. I thought to something in a way like

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号