(转)按字节长度截取字符串(支持截取带HTML代码样式的字符串)

/// <summary>
        
/// 按字节长度截取字符串(支持截取带HTML代码样式的字符串) 
        
/// </summary>
        
/// <param name="param">将要截取的字符串参数</param>
        
/// <param name="length">截取的字节长度</param>
        
/// <param name="end">字符串末尾补上的字符串</param>
        
/// <returns>返回截取后的字符串</returns>
        public static string StringHTML(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特殊字符,如&nbsp;
            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 = Regex.Replace(result.ToString(), "(>)[^<>]*(<?)""$1$2", RegexOptions.IgnoreCase);
            
//去掉不需要结素标记的HTML标记
            temp_result = Regex.Replace(temp_result, @"</?(area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>"
                , 
"", RegexOptions.IgnoreCase);
            
//去掉成对的HTML标记
            temp_result = Regex.Replace(temp_result, @"<([a-zA-Z]+)[^<>]*>(.*?)</\1>""", RegexOptions.IgnoreCase);
            
//用正则表达式取出标记
            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();
        }

原文:http://blog.163.com/chimingchong/blog/static/8186599200882333056222/
posted @ 2009-09-09 10:14  老蒋  阅读(700)  评论(0编辑  收藏  举报