gpt4 book ai didi

c# - C#中如何统计文本文件中的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:37 26 4
gpt4 key购买 nike

在此按钮单击事件中,我试图计算文本文件中与文本框中相同的字符串,然后在标签中显示它们的数量。我的问题是我不知道如何计算它们——我说的是 if 语句中的代码。我真的很感激任何帮助。

private void btnCalculate_Click(object sender, EventArgs e)
{
string openFileName;
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("You did not select OK");
return;
}
openFileName = ofd.FileName;
}
FileStream fs = null;
StreamReader sr = null;
try
{
fs = new FileStream("x", FileMode.Open, FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(fs);
string s = sr.ReadLine();
while (s != null)
{
s = sr.ReadLine();
}
if(s.Contains(tbFirstClub.Text))
{
s.Count = lblResult1.Text; //problem is here
}
else if(s.Contains(tbSecondClub.Text))
{
s.Count = lblResult2.Text; //problem is here
}
}
catch (IOException)
{
MessageBox.Show("Error reading file");
}
catch (Exception)
{
MessageBox.Show("Something went wrong");
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}

提前致谢。

最佳答案

 s.Count = lblResult1.Text; //problem is here

等等……你说的是……你有一个变量然后你访问它的属性(计数)然后将其设置为标签文本(lblResult1.Text)

这就是你想要做的吗?因为相反的可能性更大

关于c# - C#中如何统计文本文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com