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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 老蒋

[转]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)  评论()    收藏  举报