当前位置: 移动技术网 > IT编程>开发语言>c# > c#英文单词分类统计示例分享

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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();
        }
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网