C# 如何循环字符串并找出有多少个单词?
我需要帮助解决我的问题: 现在我有一个WPF应用程序,它的表单包括一个按钮和一个文本框 此外,我还有一个打开的文件目录,可以打开.cs和.txt文件 我需要遍历这些文件的字符串,并显示其中最常见的单词,从最大的到最小的 例如,字符串应为: “太阳是明亮的,太阳是黄色的” 将返回: 平均值=2 sun=2 is=2 亮度=1 黄色=1 我现在的代码是:C# 如何循环字符串并找出有多少个单词?,c#,C#,我需要帮助解决我的问题: 现在我有一个WPF应用程序,它的表单包括一个按钮和一个文本框 此外,我还有一个打开的文件目录,可以打开.cs和.txt文件 我需要遍历这些文件的字符串,并显示其中最常见的单词,从最大的到最小的 例如,字符串应为: “太阳是明亮的,太阳是黄色的” 将返回: 平均值=2 sun=2 is=2 亮度=1 黄色=1 我现在的代码是: private void btn1_Click(object sender, RoutedEventArgs e) { O
private void btn1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false };
if (ofd.ShowDialog() == true)
rtb.Text = File.ReadAllText(ofd.FileName);
string[] userText = rtb.Text.ToLower().Split(new char[] { ' ', ',', '=', '+', '}', '{', '\r', '\n', '(', ')', ';' }, StringSplitOptions.RemoveEmptyEntries);
var frequencies = new Dictionary<string, int>();
foreach (string word in userText) //search words in our array userText that we declared at the beginning.
{
}
}
private void btn1\u单击(对象发送方,路由目标)
{
OpenFileDialog ofd=newOpenFileDialog(){Filter=“Text Documents |*.cs;*.txt”,ValidateNames=true,Multiselect=false};
if(ofd.ShowDialog()==true)
rtb.Text=File.ReadAllText(ofd.FileName);
string[]userText=rtb.Text.ToLower().Split(新字符[]{',',','=','+','}','{','\r','\n','(',','),';'},StringSplitOptions.removeMptyEntries);
var frequencies=新字典();
foreach(userText中的stringword)//搜索我们在开始时声明的数组userText中的单词。
{
}
}
我不知道如何继续从这里。。。非常感谢您的帮助。这听起来像是字典的原始定义,因此这可能是一个很好的起点:
IDictionary<string, int> actualDictionary = new Dictionary<string, int>();
IDictionary actualDictionary=new Dictionary();
您可以将单词放入词典中,并在每次找到它们时增加它们的计数
IDictionary<string, int> actualDictionary = new Dictionary<string, int>();
foreach (string word in userText) //search words in our array userText that we declared at the beginning.
{
if (!actualDictionary.ContainsKey(word)) {
actualDictionary[word] = 0;
}
actualDictionary[word]++;
}
foreach(var thing in actualDictionary) {
Console.WriteLine(thing.Key + " " + thing.Value);
}
IDictionary actualDictionary=new Dictionary();
foreach(userText中的stringword)//搜索我们在开始时声明的数组userText中的单词。
{
如果(!actualDictionary.ContainsKey(word)){
实际字典[字]=0;
}
实际词典[单词]+;
}
foreach(实际词典中的var thing){
Console.WriteLine(thing.Key+“”+thing.Value);
}
请参阅上的运行示例。使用您为我们提供的示例和预期输出,我可以通过使用
.GroupBy
以及创建一个匿名类来实现这一点
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// example string
var myString = "The sun is bright. The sun is yellow";
// split the string into an array based on space characters and the period
var stringArray = myString.Split(new char[] {' ', '.'}, StringSplitOptions.RemoveEmptyEntries);
// group the items in the array based on the value, then create an anonymous class with the properties `Word` and `Count`
var groupedWords = stringArray.GroupBy(x => x).Select(x => new { Word = x.Key, Count = x.Count() }).ToList();
// print the properties based on order of largest to smallest count to screen
foreach(var item in groupedWords.OrderByDescending(x => x.Count))
{
Console.WriteLine(item.Word + " = " + item.Count);
}
// Output
---------
// The = 2
// sun = 2
// is = 2
// bright = 1
// yellow = 1
}
}
让我知道这是否有帮助 我会选择@GTown Coder的方法作为最简单的方法。但是如果您真的只想知道如何使用字典实现与示例中相同的代码
private void btn1\u单击(对象发送方,路由目标)
{
OpenFileDialog ofd=newOpenFileDialog(){Filter=“Text Documents |*.cs;*.txt”,ValidateNames=true,Multiselect=false};
if(ofd.ShowDialog()==true)
rtb.Text=File.ReadAllText(ofd.FileName);
string[]userText=rtb.Text.ToLower().Split(新字符[]{',',','=','+','}','{','\r','\n','(',','),';'},StringSplitOptions.removeMptyEntries);
var frequencies=新字典();
foreach(userText中的stringword)//搜索我们在开始时声明的数组userText中的单词。
{
//样品在这里
if(频率包含(字))
{
频率[字]+;
}
其他的
{
增加(单词1);
}
}
foreach(频率中的var kvp)
WriteLine(“单词:{0}\t频率:{1}”,kvp.Key,kvp.Value);
}
< /代码>我会考虑让你<代码>字典<代码>大小写不敏感……代码>var频率=新字典(StringComparer.CurrentCultureInoRecase)代码>感谢您的解决方案!如何准确地将返回值放入文本框?如果您有一些文本框txt
,则只需设置txt.Text
字段txt.Text=string.Join(“\r”,frequencies.Select(kvp=>$”单词:{kvp.Key}频率:{kvp.Value}”).ToArray()
。尽管您最好使用ListBox并将每一行分配给ListBoxItem。