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

前端 未结 6 1961
闹比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条回答
  •  萌比男神i
    2021-02-13 05:02

    you could convert the string to a char array with "ToCharArray();" but i don't think it will improve the performance.. you could try to use unsafe code (pointer) instead of for but well that has its drawbacks to.

提交回复
热议问题