Posts Tagged character

Count occurances of a character in a string

Posted by Ozer Senturk on Tuesday, 9 March, 2010
/// <summary>
/// Counts the number of occurrances of character chr in string str
/// </summary>
/// <param name="str">The string to count character chr in</param>
/// <param name="chr">The character to count in str</param>
/// <returns>The number of occurrances of character chr in string str</returns>
public static int countOccurances(String str, Char chr)
{
    int count = 0;

    for (int i = 0; i < str.Length; i++)
        if (str[i] == chr)
            count++;

    return count;
}