字符串截取,字符串过滤HTML标记,字符串过滤脚本,C#字符串处理类

namespace Lance
{
    /// <summary>
    /// 字符串处理
    /// </summary>
    public class StringHandle
    {
        /// <summary>
        /// 剪切字符串
        /// </summary>
        /// <param name="inputString">要剪切的字符串</param>
        /// <param name="len">要保留字符的字节数</param>
        /// <returns></returns>
        public static string CutString(string inputString, int len)
        {

            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen > len)
                    break;
            }
            //如果截过则加上半个省略号 
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
            {
                if (tempString.Length > 3)
                {
                    tempString = tempString.Substring(0, tempString.Length - 3);
                }
                tempString += "...";
            }
            return tempString;
        }
        //字符串过滤HTML标记
        public static string GetTextFromHTML(string HTML)
        {
            Regex regEx = new System.Text.RegularExpressions.Regex(@"<\/*[^<>]*>", RegexOptions.IgnoreCase);
            return regEx.Replace(HTML, "");
        }
        //字符串过滤脚本
        public static string FilterScript(string content)
        {
            string regexstr = @"<script[^>]*>([\s\S](?!<script))*?</script>";
            return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);

        }


    }
}

  作者:lance 转载请保留本网址:http://www.lancego.com/NewsDetails183

 

posted @ 2013-07-25 16:10  刘满意  阅读(370)  评论(0编辑  收藏  举报