惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News
P
Palo Alto Networks Blog
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
H
Heimdal Security Blog
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 【当耐特】
Webroot Blog
Webroot Blog
小众软件
小众软件
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
Cloudbric
Cloudbric
AI
AI
WordPress大学
WordPress大学
博客园 - 聂微东
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
博客园 - Franky
V
V2EX
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
H
Help Net Security
量子位
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
C
Cisco Blogs
S
Security Affairs

博客园 - 老蒋

[转]silverlight打印慢的问题 Android模拟器访问google网站获取天气信息时,出现 java.net.UnknownHostException: www.google.com 错误 在Eclipse中运行Android程序报 Failed to write core dump. Minidumps are not enabled by default on client versions of Windows 解决方法 在Eclipse中Override基类行为的便捷方式 20110515041233(yyyyMMddHHmmss)时间格式,转换成yyyy-MM-dd HH:mm:ss 未能从程序集 D:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.Build.Tasks.dll 加载任务“Microsoft.Build.Tasks.Delete”。 - 老蒋 [javascript]解决IE7的window.close()弹出确认提示框(转) - 老蒋 - 博客园 【转】使用ASP.NET 2.0新增加的SetFocus和MaintainScrollPositionOnPostback增强用户体验 - 老蒋 - 博客园 清除数据库日志文件 asp.net 导出Excel时,解决纯数字字符串变成类似这样的 2.00908E+18 形式的代码 - 老蒋 (实战)使用ExtJs+WCF打造分行信息管理功能的一些心得 此数据库没有有效所有者,因此无法安装数据库关系图支持对象。若要继续,请首先使用“数据库属性”对话框的“文件”页或 ALTER AUTHORIZATION 语句将数据库所有者设置为有效登录名,然后再添加数据库关系图支持对象。 FileUpload控件IIS7上传限制设定方法 - 老蒋 - 博客园 IIS7.0 检测到在集成的托管管道模式下不适用的ASP.NET设置 的解决方法 江西人(一)---简介 使用C#打造自己的mp3播放器(基于Windows Media Player内核) 请教开发WinForm时输入法问题 WebPart使用问题 使用MD5加密注册用户密码的简单示例 - 老蒋 - 博客园
(转)按字节长度截取字符串(支持截取带HTML代码样式的字符串)
老蒋 · 2009-09-09 · via 博客园 - 老蒋

/// <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  老蒋  阅读(707)  评论()    收藏  举报