我的代码如下所示。我希望得到一个排序列表,每个单词显示段落“s”中的时间。最终列表假设orderbydecending“times”。
void Main()
{
string s = "Using the Thread class The Thread class can be found in the System.Threading namespace. This class enables you to create new treads, manage their priority, and get their status. The Thread class isn’t something that you should use in your applications, except when you have special needs. However, when using the Thread class you have control over all configuration options. You can, for example, specify the priority of your thread, tell Windows that your thread is long running, or configure other advanced options. Listing 1-1 shows an example of using the Thread class to run a method on another thread. The Console class synchronizes the use of the output stream for you so you can write to it from multiple threads. Synchronization is the mechanism of ensuring that two threads don’t execute a specific portion of your program at the same time. In the case of a console application, this means that no two threads can write data to the screen at the exact same time. If one thread is working with the output stream, other threads will have to wait before it’s finished.";
var a = s.Split(new char[]{' ', ',', '<', '.', '>', '/', '?', ':', ';', '\"', '\'', '\\', '|', '{', '[', '}', ']', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', ' '});
var q1 = a.Where(o => !o.IsEmpty());
var q2 = q1.Distinct();
var al = new ArrayList();
foreach(var w in q2)
{
al.Add(new result{ times = GetTimes(q1, w), word = w});
}
al.Dump();
}
public Func<IEnumerable<string>, string, int> GetTimes = (arg1, arg2) =>
{
var rst = arg1.Count(o => o == arg2);
return rst;
};
public class result
{
public int times {get; set;}
public string word {get; set;}
}
答案 0 :(得分:1)
好像,你想要这样的东西
string s = "Using the Thread class The Thread class can be found in the System.Threading namespace. This class enables you to create new treads, manage their priority, and get their status. The Thread class isn’t something that you should use in your applications, except when you have special needs. However, when using the Thread class you have control over all configuration options. You can, for example, specify the priority of your thread, tell Windows that your thread is long running, or configure other advanced options. Listing 1-1 shows an example of using the Thread class to run a method on another thread. The Console class synchronizes the use of the output stream for you so you can write to it from multiple threads. Synchronization is the mechanism of ensuring that two threads don’t execute a specific portion of your program at the same time. In the case of a console application, this means that no two threads can write data to the screen at the exact same time. If one thread is working with the output stream, other threads will have to wait before it’s finished.";
var a = s.Split(" ,<.>/?:;\"'\\|{[}]`~!@#$%^&*()-_+=".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
var list = a.Distinct()
.Select(x => new result { times = a.Count(y => y == x), word = x })
.OrderByDescending(x => x.times)
.ToList();