zoukankan      html  css  js  c++  java
  • 字符串截取,字符串过滤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[^>]*>([sS](?!<script))*?</script>";
                return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
    
            }
    
    
        }
    }
    

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

  • 相关阅读:
    线段树
    数学建模中的excel操作
    POJ 3666 Making the Grade
    POJ 1742 Coins
    CF 55D
    POJ 3280 Cheapest Palindrome
    牛客 处女座与复读机
    牛客 处女座的约会
    牛客 小a与星际探索
    POJ 2229 递推
  • 原文地址:https://www.cnblogs.com/lmy213/p/3214562.html
Copyright © 2011-2022 走看看