源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#获取关键字附近文字算法实例

  • 时间:2022-09-25 08:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#获取关键字附近文字算法实例
本文实例讲述了C#获取关键字附近文字算法。分享给大家供大家参考。具体如下: [b]算法描述:[/b] 1.将文章以字符串的形式传入。 2.用正则表达式进行匹配。 3.在匹配中返回关键字附近的文件。 4.知道匹配结束。 [b]流程图如下:[/b] [img]http://files.jb51.net/file_images/article/201507/2015722121439941.jpg?2015622121459[/img]
public string GetLeng(string st)
{
  string s = "";
  int i = 1;
  string key = Request.QueryString["KeyWord"].ToString();
  Regex reg = new Regex(key);
  Match mat = reg.Match(st);
  while (mat.Success)
  {
   if (mat.Index - 15 > 0 && mat.Index + 15 < st.Length)
   {
    s = s + st.Substring(mat.Index - 15, 30);
    // MessageBox.Show(mat.Index.ToString());//位置 
    mat = reg.Match(st, mat.Index + mat.Length);
    // this.richTextBox2.AppendText(mat);
   }
   if (mat.Index == 0)
   {
    // if (mat.Index - 30 >= 0)
    //{ s = s + st.Substring(0, 30);}
    //else
    // {
     s = s + st.Substring(0, st.Length);
    //}
   }
    if (mat.Index == st.Length - key.Length)
   {
     s = s + st.Substring(0, 30);
   }
  }
}

希望本文所述对大家的C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部