开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜