[Solved] C# Most performant way of counting sections in a string, by delimiter


The fastest way would definitely be to go through the string character by character and count the number of delimiters.

int sectionCount = 1;
foreach ( var character in inputString )
{
   if ( character == '\\' )
   {
      sectionCount++;
   }
}

Note that we start the counter with 1 to account for the first section before we find a first delimiter. In case the input has no delimiters, it has basically one section, hence the invariant holds.

5

solved C# Most performant way of counting sections in a string, by delimiter