Counting how many times a certain char appears in a string before any other char appears

前端 未结 12 891
再見小時候
再見小時候 2021-01-03 17:17

I have many strings. Each string is prepended with at least 1 $. What is the best way to loop through the chars of each string to count how many $\

12条回答
  •  耶瑟儿~
    2021-01-03 18:00

    just a simple answer:

        public static int CountChars(string myString, char myChar)
        {
            int count = 0;
            for (int i = 0; i < myString.Length; i++)
            {
                if (myString[i] == myChar) ++count;
            }
            return count;
        }
    

    Cheers! - Rick

提交回复
热议问题