我也问过类似的问题
楼主参考一下吧
http://community.csdn.net/Expert/topic/5268/5268677.xml?temp=4.540652E-02

解决方案 »

  1.   

    给你个简单的例子string str = "我爱你老虎油";
    int i = System.Text.RegularExpressions.Regex.Matches(str,"我爱你").Count;
    //i即是我爱你在文章中出现的次数
      

  2.   

    这段代码你可以参考一下呵://分行搜索
    private void MenuItemSearchLine_Click(object sender, EventArgs e)
            {
                string[] strsRegs = new string[100];
                char[] n = { '\n' };            if (TextBox.Text.Length != 0)   //TextBox.Text是显示文章的地方
                {
                    strsRegs = AcceptStr.Split(n);  //AcceptStr是你要搜索的关键字                FileStream file = new FileStream(filename, FileMode.Open);  //filename是你要搜索的文章
                    StreamReader sr = new StreamReader(file, ansi);                Dictionary<string, int> key = new Dictionary<string, int>();  //记录关键字和它的次数
                    int i = 1;//记录行数                string Strline;
                    Strline = sr.ReadLine();
                    while (Strline != null)
                    {
                        foreach (string strReg in strsRegs)
                        {
                            if (!strReg.Equals(""))
                            {
                                
                                key.Add(strReg, 0);                            Regex regex = new Regex(strReg);
                                MatchCollection mc = regex.Matches(Strline);
                                if (mc.Count > 0)
                                {
                                    for (int j = 0; j < mc.Count; j++)
                                    {
                                        key[strReg]++;  //
                                    }
                                }
                                richTextBoxResult.Text += "关键字:" + strReg + "在第" + i + "行出现" + key[strReg] + "次。\n";
                            }
                        }
                        Strline = sr.ReadLine();
                        i++;
                        key.Clear();
                    }
                    sr.Close();
                }
            }