Re: Count all occurrences of a character in a string

From: Paul E Collins (find_my_real_address_at_CL4.org)
Date: 12/06/04


Date: Mon, 6 Dec 2004 16:35:26 +0000 (UTC)


"thomaz" <suppa@nce.ufrj.br> wrote:

> There is a string method to count the total number
> of a specified character in a string.

private static int CountChar(char c, string s)
{
   int pos = 0, count = 0;

   while ((pos = s.IndexOf(c, pos)) != -1)
   {
      count++;
      pos++;
   }

   return count;
}

P.