String扩展方法

namespace CSharpConsole
{
    /// <summary>
    /// 使用String扩展方法返回字符串中所含空格的个数
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            string a = "a b s c e";
            int acount = a.MatcheCount();
            Console.Write(acount);
            Console.ReadKey();
        }
    }

    public static class StringExtentsion
    {
        public static int MatcheCount(this string str)
        {
            string regStr = @"\s";
            Regex reg = new Regex(regStr, RegexOptions.Singleline);
            return reg.Matches(str).Count;
        }
    }
}

  

posted @ 2016-01-13 11:31  马羊  阅读(208)  评论(0)    收藏  举报