What is the fastest way to count newlines in a .NET string?
- by Cheeso
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++.