首页 > 其他 > 详细

08-在字符串中统计子串出现的次数

时间:2015-03-05 10:33:26      阅读:269      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace _05在字符串中统计子串出现的次数
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = SubstringCount("qhwerzdfrefysasefhzylmjyfzhy723hzy84hd63", "hzy");//一般方法
            //int count = RegexCount("qhwerzdfrefysasefhzylmjyfzhy723hzy84hd63");//正则方法
            Console.WriteLine(count);
            Console.ReadKey();
        }

        static int SubstringCount(string str, string substring)
        {
            if (str.Contains(substring))
            {
                string strReplaced = str.Replace(substring, "");
                return (str.Length - strReplaced.Length) / substring.Length;
            }
            return 0;
        }

        static int RegexCount(string str)
        {
            string regStr = "(hzy)";
            MatchCollection matchs = Regex.Matches(str, regStr);
            return matchs.Count;
        }
    }
}

 

08-在字符串中统计子串出现的次数

原文:http://www.cnblogs.com/zy-style/p/4315033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!