C# 如何通过忽略逗号c来拆分字符串?
我做了一个小项目,它接收.cs文件,读取它们并返回文件中最常用的单词。但是,现在它返回最常见的单词是逗号。 如何使拆分字符串时忽略逗号 例如:我有一个字符串: ?a a,b cdef cfed,abef abef abef 现在,它返回最常见的单词是'abef',并且出现了2次(程序不计算第三个abef,即最后带有逗号的一个) 另一个例子: ?a、b、cdef、cfed、abef、abef、abef、 现在返回最常见的单词是逗号',它出现了3次,但问题是-我希望我的程序忽略逗号,只关注单词C# 如何通过忽略逗号c来拆分字符串?,c#,.net,split,C#,.net,Split,我做了一个小项目,它接收.cs文件,读取它们并返回文件中最常用的单词。但是,现在它返回最常见的单词是逗号。 如何使拆分字符串时忽略逗号 例如:我有一个字符串: ?a a,b cdef cfed,abef abef abef 现在,它返回最常见的单词是'abef',并且出现了2次(程序不计算第三个abef,即最后带有逗号的一个) 另一个例子: ?a、b、cdef、cfed、abef、abef、abef、 现在返回最常见的单词是逗号',它出现了3次,但问题是-我希望我的程序忽略逗号,只关注单词 na
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false }) //openfiledialog (all .cs; all.txt)
{
if (ofd.ShowDialog() == DialogResult.OK) //if in file dialog a file gets selected
{
using (StreamReader sr = new StreamReader(ofd.FileName)) //text reader
{
richTextBox1.Text = await sr.ReadToEndAsync(); //reads the file and returns it into textbox
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string[] userText = richTextBox1.Text.ToLower().Split( ' ' );
var frequencies = new Dictionary<string, int>(); // variable frequencies, dictionary with key string, value int.
string highestWord = null; //declare string highestword with starting value null.
int highestFreq = 0; //declare integer highestfreq with starting value zero.
foreach (string word in userText) //search words in our array userText that we declared at the beginning.
{
int freq; //declare integer freq.
frequencies.TryGetValue(word, out freq); //trygetvalue from dictionary key, out value.
freq += 1; //count it.
if (freq > highestFreq)
{
highestFreq = freq;
highestWord = word;
}
frequencies[word] = freq; //assign most frequent word in frequencies dictionary to freq
}
MessageBox.Show("the most occuring word is: " + highestWord + ", it occured " + highestFreq + " times"); //display data to messagebox.
}
}
}
命名空间窗口窗体SAP8
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有异步无效按钮1\u单击(对象发送方,事件参数e)
{
使用(OpenFileDialog ofd=new OpenFileDialog(){Filter=“Text Documents |*.cs;*.txt”,ValidateNames=true,Multiselect=false})//OpenFileDialog(all.cs;all.txt)
{
if(ofd.ShowDialog()==DialogResult.OK)//如果在文件对话框中选择了文件
{
使用(StreamReader sr=newstreamreader(ofd.FileName))//文本读取器
{
richTextBox1.Text=wait sr.ReadToEndAsync();//读取文件并将其返回到textbox
}
}
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
字符串[]userText=richTextBox1.Text.ToLower().Split(“”);
var frequencies=new Dictionary();//可变频率,带键字符串的字典,值int。
string highestWord=null;//声明起始值为null的字符串highestWord。
int highestFreq=0;//声明起始值为零的整数highestFreq。
foreach(userText中的stringword)//搜索我们在开始时声明的数组userText中的单词。
{
int freq;//声明整数频率。
TryGetValue(word,out freq);//字典键中的TryGetValue,out value。
freq+=1;//数一数。
中频(频率>最高频率)
{
最高频率=频率;
最高的单词=单词;
}
frequencies[word]=freq;//将频率字典中最频繁的单词分配给freq
}
Show(“最常出现的单词是:“+highestWord+”,它出现了“+highestFreq+”次”);//向MessageBox显示数据。
}
}
}
您可以用空字符串替换逗号,然后通过算法运行输出
string original = ", . ? a a, b cdef cfed, abef abef abef,";
string noCommas = original.Replace(",", string.Empty);
参考:
拆分可以使用一组字符进行拆分。所以你可以在空格和逗号上分开。然后使用适当的StringSplit选项删除空条目
string[] userText = richTextBox1.Text.ToLower().Split(new char[] { ' ', ','}, StringSplitOptions.RemoveEmptyEntries );
您还可以使用Linq来计算一个单词的频率,代码如下
var g = userText.GroupBy(x => x)
.Select(z => new
{ word = z.Key, count = z.Count()})
.ToList();
string mostUsed = g.OrderByDescending(x => x.count)
.Select(x => x.word)
.FirstOrDefault();
另一个选项是通过使用正则表达式使拆分更易于扩展,更具体地说:
string input = ", . ? a a, b cdef cfed, abef abef abef, , ,";
string[] result = Regex.Split(input, @"\w+");
在这里检查
如果?
是有效单词,则正则表达式可以是@“\w+\124;\?”
因此,我的建议是使用regex,即使split方法现在已经足够了,因为它更强大,并且可以很容易地适应以后的更改
作为奖励,学习正则表达式非常好。向我们展示您的代码。根据您执行此操作的方式,这可能是1行代码或5行代码。抱歉,现在添加了代码yi,注释为“//declare string highestword,起始值为null。”不要提供任何值。从它旁边的代码可以明显看出,您正在声明一个具有空值的变量。这样的评论只是杂乱无章。拆分可能需要一系列字符来拆分。所以你可以在空格和逗号上分开。然后使用适当的StringSplitOptions删除空条目,并将其添加到steve所说的
var words=text.Split(新字符[]{'',','},StringSplitOptions.RemoveEmptyEntries)代码>谢谢你的解决方案,但是我偶然发现了这个问题,让我们假设空格和逗号不是我想要删除的唯一东西-我还想删除所有的花括号、点和等号-当我这样做时,string[]userText=richTextBox1.Text.ToLower().Split(新字符[]{','=','=','+','},'{},StringSplitOptions.RemoveEmptyEntries)代码>它又开始将逗号作为字符串中最常见的单词,我是否遗漏了什么?你能给出一个产生问题的字符串示例吗?不管怎样,它只是我文本框中的逗号!然而,它仍然返回“看不见的东西”是最常见的词,不知道它是什么?我使用的字符串来自.cs文件,因此我将使用pastebin,因为注释部分中有一个字符限制:(它说最常用的单词是,并且在这个字符串中出现了6次。向字符串添加一个\r和一个\n字符以进行拆分。现在效果很好!谢谢!这些是新行字符?(当您按enter键时出现)我想。