首页 > 学院 > 开发设计 > 正文

csharp:search and Compare string

2019-11-17 02:34:36
字体:
来源:转载
供稿:网友

csharp:search and Compare string

 /// <summary>        /// 涂聚文        /// 2011 捷为工作室        /// 缔友计算机信息技术有限公司        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        PRotected void Page_Load(object sender, EventArgs e)        {            this.Page.Title = "字符串操作";            StringBuilder search =new StringBuilder();            //查找以a字母至多5个字符放在匹配字边界            string text = @"This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with asp.net. entity revised and updated for the 3.5 release of .net, this book will give you the information you need to master asp.net and build a dynamic, successful,enterprsie web application.";            string pattern = @"/ba";///b字边界,/B 不是字边界的位界 这是特殊字符或转义序列            MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);            //WriteMatches(text, matches);            Response.Write("Original text was:" + text + "<br/>");            Response.Write("No.of matches:" + matches.Count.ToString() + "<br/>");            foreach (Match nextMatch in matches)            {                int Index = nextMatch.Index;                string result = nextMatch.ToString();                int charsBefore = (Index < 5) ? Index : 5;                int formEnd = text.Length - Index - result.Length;                int charsAfter = (formEnd < 5) ? formEnd : 5;                int charsToDisplay = charsBefore + charsAfter + result.Length;                //Console.WriteLine("index:{0},/t string:{i},/t {2}", Index, result, text.Substring(Index - charsBefore, charsToDisplay));                Response.Write("Index:" + Index);                Response.Write(",  String:" + result);                Response.Write(",..." + text.Substring(Index - charsBefore, charsToDisplay).Replace("a", "<font color=red>a</font>") + "...<br/>");                search.Append(text.Substring(Index - charsBefore, charsToDisplay).Replace("a", "<font color=red>a</font>"));            }            #region 得出结果            /*             No.of matches:10            Index:39, String:a,ides a broa            Index:47, String:a,road and th            Index:77, String:a,n of all as            Index:81, String:a, all aspect            Index:109, String:a,with asp.ne            Index:133, String:a,ised and up            Index:233, String:a,ster asp.ne            Index:241, String:a,.net and bu            Index:251, String:a,uild a dyna            Index:288, String:a, web applic                          */            #endregion                        Response.Write(search.ToString());            Response.Write("...<br/>");            Response.Write(SeachText(text,"a",5, pattern));        }        /// <summary>        /// 搜索文章内容得到搜索关键字截取显示        /// </summary>        /// <param name="text"></param>        /// <param name="strWord"></param>        /// <returns></returns>        private string SeachText(string text, string strword, int num, string pattern)        {            StringBuilder search = new StringBuilder();            //string text = @"This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with asp.net. entity revised and updated for the 3.5 release of .net, this book will give you the information you need to master asp.net and build a dynamic, successful,enterprsie web application.";            //string pattern = @"/ba";///b字边界,/B 不是字边界的位界 这是特殊字符或转义序列            MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);            //Response.Write("Original text was:" + text + "<br/>");            //Response.Write("No.of matches:" + matches.Count.ToString() + "<br/>");            foreach (Match nextMatch in matches)            {                int Index = nextMatch.Index;                string result = nextMatch.ToString();                int charsBefore = (Index < num) ? Index : num;                int formEnd = text.Length - Index - result.Length;                int charsAfter = (formEnd < num) ? formEnd : num;                int charsToDisplay = charsBefore + charsAfter + result.Length;                //Console.WriteLine("index:{0},/t string:{i},/t {2}", Index, result, text.Substring(Index - charsBefore, charsToDisplay));                //Response.Write("Index:" + Index);                //Response.Write(",  String:" + result);                //Response.Write(",..." + text.Substring(Index - charsBefore, charsToDisplay).Replace(strword, "<font color=red>a</font>") + "...<br/>");                string reword ="<span style='color:#FF0000;'>"+ strword +"</span>";// "<font color=red>" + strword + "</font>";                search.Append(text.Substring(Index - charsBefore, charsToDisplay).Replace(strword, reword));            }            return search.ToString();        }        /// <summary>        /// 首先是前台调用:strBuilder.Append ( GetLogExtract ( LogText, WordNum, LogUrl ) );        /// </summary>        /// <param name="LogText"></param>        /// <param name="WordNum"></param>        /// <param name="LogUrl"></param>        /// <returns></returns>        public string GetLogExtract ( string LogText, int WordNum, string LogUrl )        {            string LogExtract=LogText;            if ( LogText.IndexOf ( "#此前内容作为摘要#" )>0 )            {                LogExtract = LogText.Substring ( 0, LogText.IndexOf ( "#此前内容作为摘要#" ) ) + "&hellip;…<a href=/""+LogUrl+"/">阅读全文>>></a>";            }            else            {                int Contentlen = GetStrLength ( LogText );                if ( Contentlen <= WordNum )                {                    LogExtract = LogText;                }                else                {                    if ( LogText.LastIndexOf ( "<object" ) >0 || LogText.LastIndexOf ( "<OBJECT" ) >0 )                    {                        if ( WordNum <100 )                        {                            LogExtract="";                        }                        else                        {                            LogExtract = WipeOffTableHTML ( LogText );                        }                    }                    else                    {                        LogExtract = InterceptStr (WipeOffTableHTML ( LogText ), WordNum+100 );                        if ( LogExtract.LastIndexOf ( "<p" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<p" ))<400 )                        {                            LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<p" ) -1 );                        }                        else if ( LogExtract.LastIndexOf ( "<img" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<img" ))<400 )                        {                            LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<img" ) -1 );                        }                        else if ( LogExtract.LastIndexOf ( "。" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "。" ))<400 )                        {                            LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "。" ) );                        }                        else if ( LogExtract.LastIndexOf ( "<br" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<br" ))<400 )                        {                            LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<br" ) -1 );                        }                        else if ( LogExtract.LastIndexOf ( "?" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "?" ))<400 )                        {                            LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "?" ) );                        }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表