c#英文单词分类统计示例分享
程序员文章站
2024-02-24 08:20:10
复制代码 代码如下:using system;using system.linq;namespace consoleapplication1{ &nb...
复制代码 代码如下:
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();
}
}
}