求给文章关键字加链接函数[WEB学习示例]

问题来源于http://topic.csdn.net/u/20091106/22/856d3224-67d1-46ee-807b-17e00385cf57.html?seed=1865148156&r=61014592#r_61014592

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

 

public partial class Default2 : System.Web.UI.Page
{
    string nr = "";

 

    protected void Page_Load(object sender, EventArgs e)
    {
        nr = "我想学习C语言教程,我想看的是C语言视频教程,我喜欢C语言,我想习学C语言教程 ,其它什么C语言教程,我都不想看。";
        string[] a1 = { "C语言视频教程", "C语言教程", "C语言" };//英文字母区分大小写
        string[] a2 = { "a1.html", "a2.html", "a3.html" };
        for (int Th = 0; Th < a1.Length; Th++)
        { TiHuan(nr, a1[Th], a2[Th]); }
    }


    protected string TiHuan(string Nr, string Gjz, string Wz)
    {
        string[] NeiRong = new string[1];
        string ZuHeNeiRong = "", TiHuan = "" + Gjz + "";
        int ChanDu = 0;

        NeiRong = Nr.Split('<');//首先分割出

        for (int ii = 0; ii < NeiRong.Length; ii++)
        {
            if (NeiRong[ii].IndexOf("a ") >= 0 || NeiRong[ii].IndexOf("A ") >= 0)// || NeiRong[ii].IndexOf("/a") >= 0 || NeiRong[ii].IndexOf("/A") >= 0
            {
                ZuHeNeiRong += "<" + NeiRong[ii];//补齐字符
            }
            else//过滤掉包含指定字符串的内容得到不包含超链接的内容
            {
                ChanDu = 0;//初始化
                for (int cl = 0; cl < NeiRong[ii].Length / Gjz.Length; cl++)
                {
                    if (NeiRong[ii].Substring(ChanDu, NeiRong[ii].Length - ChanDu).IndexOf(Gjz) >= 0)//如果找到关键字
                    {
                        if (NeiRong[ii].IndexOf("/a") >= 0 || NeiRong[ii].IndexOf("/A") >= 0)
                        { ZuHeNeiRong += "<"; }//补齐字符

                        ZuHeNeiRong += NeiRong[ii].Replace(Gjz, TiHuan); //替换
                       
                        ChanDu = NeiRong[ii].IndexOf(Gjz) + Gjz.Length;//继续往后寻找
                    }
                    else
                    {
                        if (ChanDu == 0)
                        {
                            if (NeiRong[ii].IndexOf("/a") >= 0 || NeiRong[ii].IndexOf("/A") >= 0)
                            { ZuHeNeiRong += "<"; }//补齐字符

                            ZuHeNeiRong += NeiRong[ii]; break;
                        }
                    }
                   
                }
            }
        }
        Label1.Text = ZuHeNeiRong;
        nr = ZuHeNeiRong;//替换后的内容返回再次替换
        return ZuHeNeiRong;
    }

}

 

 

 

你可能感兴趣的:(求给文章关键字加链接函数[WEB学习示例])