Charecter Count in a string

Hi friends,
I haven't found any separate function in c# to find the no of occurrence of one letter in one string.

Here is a simple function to find out no of occurrence of a character in string easily

static int CountChars(string value,char key)
int count = 0;
foreach (char c in value)
if (c==key)


return count;

we can pass string as value and letters as key and no of appearance will be returned by the function.

if u have any other idea please share with me


shyamal swaroop k.s


Author: vineet25 May 2010 Member Level: Gold   Points : 1


I have used this function and it is working quiet well.
Keep posting


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: