• 字符串截取,字符串过滤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

  • 相关阅读:
    redis学习--Hashes数据类型
    redis学习--String数据类型。
    redis学习一
    redis命令大全
    MongoDB学习笔记(索引)
    ECharts的使用(经典博客)
    php中五种常见的设计模式
    实用的借口
    php中socket的使用
    jquery仿凡客诚品图片切换的效果实例代码
  • 原文地址:https://www.cnblogs.com/lmy213/p/3214562.html
Copyright © 2020-2023  润新知