八种方法计算字符串中特定字符的数量

从网络上总结的,便于以后的学习(*^__^*) 嘻嘻……

源文件:http://pan.baidu.com/share/link?shareid=432792&uk=3912660076

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace 求字符串中某个字符的数量
{
    /// <summary>
    /// 对于每个方法加个判断
    /// str.IndexOf(char arg)>0 ?
    /// 最好!!!
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("默认字符串:");
            Console.WriteLine("0.Bugs Bug 0.Bugs Bug 1.Bugs Bug 1.Bugs Bug 2.Bugs Bug ");
            string str = "0.Bugs Bug 0.Bugs Bug 1.Bugs Bug 1.Bugs Bug 2.Bugs Bug ";
            Console.WriteLine();
            Console.WriteLine("字符串'B'的数量为{0}", CountOne(str));//10
            Console.WriteLine();
            Console.WriteLine("字符串'u'的数量为{0}", CountTwo(str));//10
            Console.WriteLine();
            Console.WriteLine("字符串'g'的数量为{0}", CountThree(str));//10
            Console.WriteLine();
            Console.WriteLine("字符串's'的数量为{0}", CountFour(str));//5
            Console.WriteLine();
            Console.WriteLine("字符串' '的数量为{0}", CountFive(str));//10
            Console.WriteLine();
            Console.WriteLine("字符串'0'的数量为{0}", CountSix(str));//2
            Console.WriteLine();
            Console.WriteLine("字符串'1'的数量为{0}", CountSeven(str));//2
            Console.WriteLine();
            Console.WriteLine("字符串'2'的数量为{0}", CountEight(str, 0));//1
            Console.WriteLine();
            Console.ReadKey();
        }
        public static int CountOne(string str)
        {
            int count = 0;
            //split返回string[],string[].Length与Count('B')是减1关系
            count = str.Split('B').Length - 1;
            return count;
        }
        public static int CountTwo(string str)
        {
            int count = 0;//Lambda 
            //http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
            count = str.Count(u => u == 'u');
            return count;
        }
        public static int CountThree(string str)
        {
            int count = 0;//长减短
            count = str.Length - str.Replace("g", "").Length;
            return count;
        }
        public static int CountFour(string str)
        {
            int count = 0;//正则
            count = Regex.Matches(str, @"s").Count;
            return count;
        }
        public static int CountFive(string str)
        {
            int count = 0;//遍历
            foreach (char item in str)
            {
                if (item == ' ')
                {
                    count++;
                }
            }
            return count;
        }

        public static int CountSix(string str)
        {
            int count = 0;//IEnumerable
            count = str.ToCharArray().Count(help);
            return count;
        }
        public static bool help(char str)
        {
            if (str == '0')
                return true;
            return false;
        }

        public static int CountSeven(string str)
        {
            int count = 0;//linq
            count = str.ToCharArray().Where(i => i.Equals('1')).ToArray().Length;
            return count;
        }

        public static int CountEight(string str, int count8)
        {
            if (str.IndexOf('2') > 0)//recursion
            {
                count8++;
                string strArg = str.Remove(0, str.IndexOf('2'));
                CountEight(strArg, count8);
            }
            return count8;
        }
    }
}

原文地址:https://www.cnblogs.com/wjshan0808/p/3049718.html