Count number of chars in a string

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
public class MainClass{

        public static int NumberOfCharsInStr(ref string stringToSearch, char charToFind)
        {
            int count = 0;
            char[] chars = stringToSearch.ToCharArray();
            foreach (char c in chars)
            {
                if (c == charToFind)
                {
                    count++;
                }
            }
            return count;
        }
}

   
     


This entry was posted in Data Types. Bookmark the permalink.