Count String Occurences - CSharp System

CSharp examples for System:String Search

Description

Count String Occurences

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Collections;
using System;/*from   w  w w . j av  a 2  s. c  om*/

public class Main{
        public static int CountOccurences(this string str, char chr, int startIndex, int count)
        {
            int occurrences = 0;
            for (int i = startIndex; i < startIndex + count; i++)
            {
                if (str[i] == chr)
                {
                    occurrences++;
                }
            }
            return occurrences;
        }
        public static int CountOccurences(this string str, char chr)
        {
            return str.CountOccurences(chr, 0, str.Length);
        }
}

Related Tutorials