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

Posted by Cheeso on Stack Overflow See other posts from Stack Overflow or by Cheeso
Published on 2010-03-31T22:43:26Z Indexed on 2010/03/31 23:03 UTC
Read the original article Hit count: 307

Filed under:
|
|

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++)
    {
        if (s[i] == '\n') c++;
    }
    return c;
}

I'm particularly concerned about the Item accessor on the string. Not sure if it is just pointer arithmetic like C/C++.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET