Counts total number of a char or chars in a string - CSharp System

CSharp examples for System:Char

Description

Counts total number of a char or chars in a string

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;// w  ww .  ja  v  a 2s. co  m

public class Main{
        //Counts total number of a char or chars in a string
        //hello, l -> 2
        //hello, el -> 1
        public static int CountTotal(string input, string chars, bool ignoreCases)
        {
            int count = 0;
            for (int i = 0; i < input.Length; i++)
            {
                if (!(i + chars.Length > input.Length) &&
                    String.Compare(input.Substring(i, chars.Length), chars, ignoreCases) == 0)
                {
                    count++;
                }
            }
            return count;
        }
}

Related Tutorials