开发中一些实用的方法

 

开发中一些实用的方法
 1.截取字符串

       #region ---截取字符串---
        /// <summary>
        /// 截取字符串
        /// </summary>
        public static string CutString(string stringToSub, int length)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    nLength += 2;
                }
                else
                {
                    nLength = nLength + 1;
                }

                if (nLength <= length)
                {
                    sb.Append(stringChar[i]);
                }
                else
                {
                    break;
                }
            }
            if (sb.ToString() != stringToSub)
            {
                sb.Append("...");
            }
            return sb.ToString();
        }
        #endregion

2.当页面内有第三方的编辑控件时,去掉编辑控件中的HTML标记

       #region ---去除HTML标记---
        /// <summary>
        /// 去除HTML标记
        /// </summary>
        /// <param name="strHtml">包括HTML的源码 </param>
        /// <returns>已经去除后的文字</returns>
        public static string NoHTML(string strHtml)
        {return NoHTML(strHtml,null);}
        /// <summary>
        /// 去除HTML标记
        /// </summary>
        /// <param name="strHtml">包括HTML的源码 </param>
        /// <param name="OtherHtmlElement">配置文件中要去除的HTML标记</param>
        /// <returns>已经去除后的文字</returns>
        public static string NoHTML(string strHtml, string OtherHtmlElement)
        {
            List<string> aryReg = new List<string>();
            aryReg.Add(@"<script[^>]*?>.*?</script>");
            aryReg.Add(@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>");
            aryReg.Add(@"([\r\n])[\s]+");
            aryReg.Add(@"&(quot|#34);");
            aryReg.Add(@"&(amp|#38);");
            aryReg.Add(@"&(lt|#60);");
            aryReg.Add(@"&(gt|#62);");
            aryReg.Add(@"&(nbsp|#160);");
            aryReg.Add(@"&(iexcl|#161);");
            aryReg.Add(@"&(cent|#162);");
            aryReg.Add(@"&(pound|#163);");
            aryReg.Add(@"&(copy|#169);");
            aryReg.Add(@"&#(\d+);");
            aryReg.Add(@"-->");
            aryReg.Add(@"<!--.*\n");    

            List<string> aryRep = new List<string>();
            aryRep.Add("");
            aryRep.Add("");
            aryRep.Add("");
            aryRep.Add("\"");
            aryRep.Add("&");
            aryRep.Add("<");
            aryRep.Add(">");
            aryRep.Add(" ");
            aryRep.Add("\xa1");
            aryRep.Add("\xa2");
            aryRep.Add("\xa3");
            aryRep.Add("\xa9");
            aryRep.Add("");
            aryRep.Add("\r\n");
            aryRep.Add("");

            //去除H配置文件中要去除的HTML标记
            try
            {
                if (OtherHtmlElement != null)
                {
                    int intValIndex;
                    string[] arrOtherHtmlElement = OtherHtmlElement.Split(new char[] { '|' });
                    for (int i = 0; i < arrOtherHtmlElement.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(arrOtherHtmlElement[i]))
                        {
                            intValIndex = arrOtherHtmlElement[i].IndexOf(",");
                            aryReg.Add(@arrOtherHtmlElement[i].Substring(0, intValIndex));
                            aryRep.Add(arrOtherHtmlElement[i].Substring(intValIndex + 1));
                        }
                    }
                }
            }
            catch { }

            string newReg = aryReg[0];
            string strOutput = strHtml;
            for (int i = 0; i < aryReg.Count; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }

            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }
        #endregion
3.对于Split方法一直都感觉用得不熟,今天总算弄明白了。
       string str = "上,下,左,右,";
        string X = ",";
        string[] strArr = str.Split(X.ToCharArray());
        Response.Write(strArr[0] + " " + strArr[1] + " " + strArr[2] + " " + strArr[3]);
这样输出结果:上 下 左 右
另外,若第三步:string[] strArr = str.Split(X.ToCharArray(),4);
这样输出的结果就变成:上 下 左 右,

 

posted @ 2009-02-22 11:59  瑞君  Views(125)  Comments(0)    收藏  举报