统计一个文本中有多少个英文单词 如何用c# 或js来实现
比如﹕ You are good ! I love you !
统计: 一共有6个单词
一个字母:1个 I
二个字母:0
三个字母:3个 You are you
四个字母:2个 good love
比如﹕ You are good ! I love you !
统计: 一共有6个单词
一个字母:1个 I
二个字母:0
三个字母:3个 You are you
四个字母:2个 good love
解决方案 »
- 用httpwebrequest方式采集某网页中的内容,为啥采集不到?
- 菜鸟问一个简单问题 。。。请问一条简单sql.
- 关于c#删除xml节点的问题
- 保存
- winxp中文下开发.net程序在Winxp英文下运行,报错:Selected collating sequence not supported by the
- [散分]
- <<高难度>>linq的映射不使用设计器也不使用sqlmetal.exe如何映射出 .dbml 文件 和如何根据dbml文件映射出 cs文件?
- 问个奇怪的问题!
- 100分:Configuration system failed to initialize
- 一段非常实用的代码,文件加密
- c#请教多个dataGridView操作数据库
- 用ICSharpCode.SharpZipLib的ZipInputStream解压文件报错
List<String> wordList = new List<String>();
Boolean isWork = true;
String word = "";
foreach (Char c in str)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
word = word + c;
isWork = true;
}
else
{
if (isWork)
{
wordList.Add(word);
word = "";
isWork = false;
}
}
}
所有信息都在wordList中了
如果你要统计不同字母个数的单词数量和具体内容,修改一下上面的程序就可以了
int count = Regex.Matches("asd eee qwe? I'm a ,,student", "[a-zA-Z]+([\']?[a-zA-Z]+)?").Count;
比如:
统计: 一共有6个因为字母字串
长度为1的字串:1个 I
长度为2的字串:0
长度为3的字串:3个 You are you
长度为4的字串:2个 good love"统计一个文本中有多少个英文单词 如何用c# 或js来实现"
如果要统计英文单词,那么就需要英文词汇表才行.
string[] array = str.Split(' ');
string value = string.Empty; Console.WriteLine("统计:一共有" + array.Where(item => item.Length > 0 && item != "!").Count() + "个单词 "); var list1 = array.Where(item => item.Length == 1);
array.Where(item => item.Length == 1); list1.ToList().ForEach(item => value = value + " " + item);
Console.WriteLine("一个字母:" + list1.Count() + "个 " + value);
value = string.Empty; var list2 = array.Where(item => item.Length == 2); list2.ToList().ForEach(item => value = value + " " + item);
Console.WriteLine("二个字母:" + list2.Count() + "个 " + value);
value = string.Empty; var list3 = array.Where(item => item.Length == 3); list3.ToList().ForEach(item => value = value + " " + item);
Console.WriteLine("三个字母:" + list3.Count() + "个 " + value);
value = string.Empty; var list4 = array.Where(item => item.Length == 4); list4.ToList().ForEach(item => value = value + " " + item);
Console.WriteLine("四个字母:" + list4.Count() + "个 " + value);
value = string.Empty;