C#判断单词个数方法总结
时间:2019-04-13
本文章向大家介绍C#判断单词个数方法总结,主要包括C#判断单词个数方法总结使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
方法一:
判断英文单词个数:
using System; namespace FindWord { class Program { static void Main(string[] args) { string space = " "; string str = "hello world" + space; int count = 0; bool start = false; for (int i=0;i<str.Length;i++) { if (Char .IsLetter(str[i])) { start = true; } if (!Char.IsLetter(str[i])&&start) { count++; start = false; } } Console.WriteLine(count); Console.ReadLine(); } } }
方法二:
C#统计英文字符串中单词个数思路如下:
1.使用的Hashtable(高效)集合,记录每个单词出现的次数
2.采用ArrayList对Hashtable中的Keys按字母序排列
3.排序使用插入排序(稳定)
public void StatisticsWords(string path) { if (!File.Exists(path)) { Console.WriteLine("文件不存在!"); return; } Hashtable ht = new Hashtable(StringComparer.OrdinalIgnoreCase); StreamReader sr = new StreamReader(path, System.Text.Encoding.UTF8); string line = sr.ReadLine(); string[] wordArr = null; int num = 0; while (line.Length > 0) { // MatchCollection mc = Regex.Matches(line, @"\b[a-z]+", RegexOptions.Compiled | RegexOptions.IgnoreCase); //foreach (Match m in mc) //{ // if (ht.ContainsKey(m.Value)) // { // num = Convert.ToInt32(ht[m.Value]) + 1; // ht[m.Value] = num; // } // else // { // ht.Add(m.Value, 1); // } //} //line = sr.ReadLine(); wordArr = line.Split(' '); foreach (string s in wordArr) { if (s.Length == 0) continue; //去除标点 line = Regex.Replace(line, @"[\p{P}*]", "", RegexOptions.Compiled); //将单词加入哈希表 if (ht.ContainsKey(s)) { num = Convert.ToInt32(ht[s]) + 1; ht[s] = num; } else { ht.Add(s, 1); } } line = sr.ReadLine(); } ArrayList keysList = new ArrayList(ht.Keys); //对Hashtable中的Keys按字母序排列 keysList.Sort(); //按次数进行插入排序【稳定排序】,所以相同次数的单词依旧是字母序 string tmp = String.Empty; int valueTmp = 0; for (int i = 1; i < keysList.Count; i++) { tmp = keysList[i].ToString(); valueTmp = (int)ht[keysList[i]];//次数 int j = i; while (j > 0 && valueTmp > (int)ht[keysList[j - 1]]) { keysList[j] = keysList[j - 1]; j--; } keysList[j] = tmp;//j=0 } //打印出来 foreach (object item in keysList) { Console.WriteLine((string)item + ":" + (string)ht[item]); } }
- redis主从集群搭建及容灾部署(哨兵sentinel)
- 浅谈ASP.NET的Postback
- redis配置详解(中英文)
- 我所理解的Remoting(1):Marshaling & Activation[下篇]
- 5分钟短视频,带你走进日志易SPL,助你日志分析更容易
- Unity Application Block 1.2 for Silverlight - December 2008
- 算法学堂 - 二分查找及其变形
- Zookeeper权限管理与Quota管理
- 刘鹏:保大还是保小?无人汽车也将面临这样的道德窘境
- 浅谈DNS
- silverlight寻奇 - Graphite
- 程序运算性能测量
- 2018年比特币的真正瓶颈在这里
- 玩转 React 服务器端渲染
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Yii2框架视图(View)操作及Layout的使用方法分析
- php实现单笔转账到支付宝功能
- PHP使用Redis实现Session共享的实现示例
- windows10在visual studio2019下配置使用openCV4.3.0
- PHP5.0 TIDY_PARSE_FILE缓冲区溢出漏洞的解决方案
- Python爬虫爬取新闻资讯案例详解
- Python代码需要缩进吗
- 解决Python paramiko 模块远程执行ssh 命令 nohup 不生效的问题
- Python计算信息熵实例
- 详解python logging日志传输
- 将tf.batch_matmul替换成tf.matmul的实现
- Python正则表达式高级使用方法汇总
- CentOS7.0下安装PHP5.6.30服务的教程详解
- Laravel Validator自定义错误返回提示消息并在前端展示
- 完美解决keras 读取多个hdf5文件进行训练的问题