17._11本章小结及任务实施

本文介绍了一个使用C#编写的程序,该程序能够去除字符串中的非中文字符,并统计剩余中文字符的出现频率。通过使用正则表达式匹配非中文字符并将其替换为空,实现了对输入字符串的清洗。然后利用Hashtable存储每个中文字符及其出现次数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections;

namespace _17._11本章小结及任务实施
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "我爱你,我的中国。我喜欢我要自学网";
            string pattern = @"[^\u4e00-\u9fa5]";
            Regex regex = new Regex(pattern);
            string chnstr = regex.Replace(str, "");//替换非中文字符
            Hashtable ht = new Hashtable();
            for(int i=0; i < chnstr.Length; i++)
            {
                int val = 1;
                if (ht.ContainsKey(chnstr[i]))
                {
                    val = Convert.ToInt32(ht[chnstr[i]]);
                    ++val;
                }
                ht[chnstr[i]] = val;
             
            }
            Console.WriteLine("原有的字符串是:" + str);
            Console.WriteLine("修剪后的字符串是:"+chnstr);
            foreach (DictionaryEntry de in ht)
            {
                Console.WriteLine("{0}----------{1}", de.Key, de.Value);
            }
            Console.Read();

        }
    }
}

 

转载于:https://www.cnblogs.com/zqyo2000z/p/5618435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值