What is the fastest way to count newlines in a large .NET string?

前端 未结 6 1980
闹比i
闹比i 2021-02-13 04:18

Is there a way to improve this:

private static int CountNewlines(string s)
{
    int len = s.Length;
    int c = 0;
    for (int i=0; i < len;  i++)
    {
           


        
6条回答
  •  你的背包
    2021-02-13 04:57

    I'm pretty sure this won't be much slower than converting the string to bytes and checking those, if not faster. The String class should be highly optimized.

    If this is a big string, maybe a parallel execution by several threads will make things faster :-)

提交回复
热议问题