首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

统计一个文本中有多少个英文单词 怎么用c# 或js来实现

2013-11-01 
统计一个文本中有多少个英文单词 如何用c# 或js来实现统计一个文本中有多少个英文单词 如何用c# 或js来实

统计一个文本中有多少个英文单词 如何用c# 或js来实现
统计一个文本中有多少个英文单词 如何用c# 或js来实现


比如﹕   You  are good !  I  love  you !


统计: 一共有6个单词
一个字母:1个  I
二个字母:0
三个字母:3个  You  are you
四个字母:2个  good love

[解决办法]
string str = "You  are good !  I  love  you !";
            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;

热点排行