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

标签 c#

在此按钮单击事件中,我试图计算文本文件中与文本框中相同的字符串,然后在标签中显示它们的数量。我的问题是我不知道如何计算它们——我说的是 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/

相关文章:

c# - 关闭分页的更简单方法

c# - mono(c#) 检测用户是否在同一网络上

c# - 如何避免在布局页面中使用 "using System.Web.Optimization"

c# - wpf手动生成TreeViewItem容器

c# - 获取 MS Word 中呈现的文本和图像的高度

c# - 如何从 Linq 读取大型 XML RAW 到 SQL?

c# - 如何使用 DoCmd.DeleteObject 删除 ms access 查询对象

c# - 在 Identity 2.0 中成功实现自定义 UserManager<IUser>

c# - 比较项目列表然后拆分

c# - 二维列表中的二进制搜索