c#英文單詞分類統計示例分享。本站提示廣大學習愛好者:(c#英文單詞分類統計示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是c#英文單詞分類統計示例分享正文
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();
}
}
}