• 截取html字符长度,并保留html格式标记


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.Text;


    public partial class test  System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str=pspan style='font-size larger'五span style='color red'大连spanspan style='font-family 黑体'池span风span style='font-family 幼圆'景区span官方网站spanspan style='font-family 宋体'span style=''span style='font-size larger'宋spanspanspanspan style='font-family 楷体_GB2312'span style='font-size larger'体spanspanp;
            Response.Write(SubstringToHTML(str, 16, …));
        }

         summary
         按字节长度截取字符串(支持截取带HTML代码样式的字符串)
         summary
         param name=param将要截取的字符串参数param
         param name=length截取的字节长度param
         param name=end字符串末尾补上的字符串param
         returns返回截取后的字符串returns
        public string SubstringToHTML(string param, int length, string end)
        {
            string Pattern = null;
            MatchCollection m = null;
            StringBuilder result = new StringBuilder();
            int n = 0;
            char temp;
            bool isCode = false; 是不是HTML代码
            bool isHTML = false; 是不是HTML特殊字符,如 
            char[] pchar = param.ToCharArray();
            for (int i = 0; i  pchar.Length; i++)
            {
                temp = pchar[i];
                if (temp == '')
                {
                    isCode = true;
                }
                else if (temp == '&')
                {
                    isHTML = true;
                }
                else if (temp == '' && isCode)
                {
                    n = n - 1;
                    isCode = false;
                }
                else if (temp == ';' && isHTML)
                {
                    isHTML = false;
                }

                if (!isCode && !isHTML)
                {
                    n = n + 1;
                    UNICODE码字符占两个字节
                    if (System.Text.Encoding.Default.GetBytes(temp + ).Length  1)
                    {
                        n = n + 1;
                    }
                }

                result.Append(temp);
                if (n = length)
                {
                    break;
                }
            }
            result.Append(end);
            取出截取字符串中的HTML标记
            string temp_result = result.ToString().Replace(()[^](), $1$2);
            去掉不需要结素标记的HTML标记
            temp_result = temp_result.Replace(@(AREABASEBASEFONTBODYBRCOLCOLGROUPDDDTFRAMEHEADHRHTMLIMGINPUTISINDEXLILINKMETAOPTIONPPARAMTBODYTDTFOOTTHTHEADTRareabasebasefontbodybrcolcolgroupdddtframeheadhrhtmlimginputisindexlilinkmetaoptionpparamtbodytdtfootththeadtr)[^],
             );
            去掉成对的HTML标记
            temp_result = temp_result.Replace(@([a-zA-Z]+)[^](.)1, $2);
            用正则表达式取出标记
            Pattern = (([a-zA-Z]+)[^]);
            m = Regex.Matches(temp_result, Pattern);
            ArrayList endHTML = new ArrayList();
            foreach (Match mt in m)
            {
                endHTML.Add(mt.Result($1));
            }
            补全不成对的HTML标记
            for (int i = endHTML.Count - 1; i = 0; i--)
            {
                result.Append();
                result.Append(endHTML[i]);
                result.Append();
            }
            return result.ToString();
        }
    }

  • 相关阅读:
    shell 的多进程
    shell 按行读取文件的内容
    2>&1的意思
    >/dev/null 2>&1
    js 变量作用域
    Premiere Pro 中的键盘快捷键
    premiere pro 2019 mac 破解
    js 空语句
    js 数组原型
    js 奇偶判断
  • 原文地址:https://www.cnblogs.com/geass/p/1923742.html
Copyright © 2020-2023  润新知