国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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 ( "?" ) );                        }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石楼县| 宁晋县| 郎溪县| 民县| 仲巴县| 喜德县| 弥勒县| 北流市| 漯河市| 仁怀市| 抚松县| 武定县| 成武县| 虞城县| 县级市| 德格县| 如皋市| 平泉县| 石河子市| 茂名市| 岳西县| 抚宁县| 上饶市| 永吉县| 加查县| 东方市| 朝阳区| 苍山县| 淮北市| 昆山市| 林芝县| 黑山县| 祥云县| 柞水县| 扬中市| 马龙县| 彭州市| 建德市| 昆明市| 洪雅县| 佛学|