统计一个文本中有多少个英文单词 如何用c# 或js来实现
比如﹕   You  are good !  I  love  you !
统计: 一共有6个单词
一个字母:1个  I
二个字母:0
三个字母:3个  You  are you
四个字母:2个  good love

解决方案 »

  1.   

    简单点用正侧 "[a-zA-Z]" 然后对果结集进行统计
      

  2.   

    我看到的是  你去掉!之后进行了分组统计..如果不小心有空格了呢 或者I'am 你怎么算?
      

  3.   

                String str = "You  are good !  I  love  you !";
                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;
                        }
                    }
                }
      

  4.   

    初始值  isWork =false ,刚才写错了
    所有信息都在wordList中了
    如果你要统计不同字母个数的单词数量和具体内容,修改一下上面的程序就可以了
      

  5.   


     int count = Regex.Matches("asd eee  qwe? I'm  a ,,student", "[a-zA-Z]+([\']?[a-zA-Z]+)?").Count;
      

  6.   

    确切的说,不应该是单词,应该是统计"英文字母字串"出现次数的统计,
    比如:
    统计: 一共有6个因为字母字串
    长度为1的字串:1个  I
    长度为2的字串:0
    长度为3的字串:3个  You  are you
    长度为4的字串:2个  good love"统计一个文本中有多少个英文单词 如何用c# 或js来实现"
    如果要统计英文单词,那么就需要英文词汇表才行.
      

  7.   

    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;