您的位置:首页 > 编程语言 > C#

c#英文单词分类统计示例分享

2018-10-12 14:00 791 查看

using System;
using System.Linq;
namespace ConsoleApplication1
{
    /// <summary>
    /// 给出一段英文,分类统计(如:长度为4的单词有2个:time,well)
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            string source = "Do one thing at a time,and do well";//已知英文语句
            string[] stringArray = source.Split(new char[] { ' ', ',' });
            var result = stringArray.GroupBy(s => s.Length).Select(s => new {
                Lenght = s.Select(x => x).FirstOrDefault().Length,
                Count = s.Count(),
                StringItems = s.Select(x => x)
            });

            foreach (var s in result)
            {
                string strResult = string.Empty;
                foreach (var item in s.StringItems)
                {
                    strResult += string.IsNullOrEmpty(strResult) ? item : " , " + item;
                }
                Console.WriteLine(string.Format("长度为{0}的单词有{1}个:{2}", s.Lenght, s.Count, strResult));
            }           
            Console.ReadKey();
        }
    }
}

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: