日期:2014-05-17  浏览次数:20521 次

在线编辑器保存到字段的内容,如何截取我要的长度?
从编辑器保存的内容,含有很多html标签代码,而我需要截取我实际内容的一部份,怎么弄?

------解决方案--------------------
C# code

        /// <summary>
        /// 过滤字符串中的html代码
        /// </summary>
        /// <param name="Str"></param>
        /// <returns>返回过滤之后的字符串</returns>
        public static string LostHTML(string Str)
        {
            string Re_Str = "";
            if (Str != null)
            {
                if (Str != string.Empty)
                {
                    string Pattern = "<\\/*[^<>]*>";
                    Re_Str = Regex.Replace(Str, Pattern, "");
                }
            }
            return (Re_Str.Replace("\\r\\n", "")).Replace("\\r", "");
        }

        #region 截取字符串为指定长度
        public static string CutStr(string sInString, int length)
        {
            if (sInString == null || sInString.Length == 0) return "";
            int iCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
            if (iCount > length)
            {
                int iLength = 0;
                for (int i = 0; i < sInString.Length; i++)
                {
                    int iCharLength = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[] { sInString[i] });
                    iLength += iCharLength;
                    if (iLength == length)
                    {
                        sInString = sInString.Substring(0, i + 1) + "...";
                        break;
                    }
                    else if (iLength > length)
                    {
                        sInString = sInString.Substring(0, i) + "...";
                        break;
                    }
                }
            }
            return sInString;
        }
        #endregion