码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-05 10:34:52      阅读:167      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!