程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp:search string,csharpstring

csharp:search string,csharpstring

編輯:C#入門知識

csharp:search string,csharpstring


 /// <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 ( "#此前內容作為摘要#" ) ) + "……<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 ( "?" ) );
                        }
                        LogExtract += "……<a href=\""+LogUrl+"\">閱讀全文>>></a>";
                    }
                }
            }
            return LogExtract;
        }


        /// <summary>
        /// 去除表格HTML標記
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns>去掉表格HTML標記後的文本</returns>
        public static string WipeOffTableHTML(string Htmlstring)
        {
            //刪除腳本
            Htmlstring = Regex.Replace(Htmlstring, @"<[^>]*?>.*?</>", "", RegexOptions.IgnoreCase);
            //刪除表格HTML
            Htmlstring = Regex.Replace(Htmlstring, @"</?table[^>]*>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</?tr[^>]*>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</?td[^>]*>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</?th[^>]*>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</?BLOCKQUOTE[^>]*>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</?tbody[^>]*>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style[^\s]*", "", RegexOptions.IgnoreCase);

            return Htmlstring;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int GetStrLength(string str)
        {
            bool WINNT_CHINESE = ("中國".Length == 2);
            if (WINNT_CHINESE)
            {
                int L, T, C;
                L = str.Length;
                T = L;
                for (int i = 1; i <= L; i++)
                {
                    Encoding ASCII = Encoding.ASCII;
                    Byte[] EncodedBytes = ASCII.GetBytes(str.Substring(i - 1, 1));
                    C = EncodedBytes[0];
                    if (C < 0)
                        C += 65536;
                    if (C > 255)
                        T += 1;
                }
                return T;
            }
            else
            {
                return str.Length;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string InterceptStr(string str, int length)
        {
            int x, y;
            str = str.Trim();
            x = str.Length;
            y = 0;
            if (x >= 1)
            {
                for (int i = 1; i <= x; i++)
                {
                    Encoding ASCII = Encoding.ASCII;
                    Byte[] EncodedBytes = ASCII.GetBytes(str.Substring(i - 1, 1));
                    if (EncodedBytes[0] < 0 || EncodedBytes[0] > 255)
                    {
                        y += 2;
                    }
                    else
                    {
                        y += 1;
                    }
                    if (y >= length)
                    {
                        str = str.Substring(0, i);
                        break;
                    }
                }
                return str;
            }
            else
            {
                return "";
            }
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved