代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
如何截取html的子字符串作为内容摘要
作者:
Dezai.CN
/ 发布于
2012/2/15
/
448
<div> <div class="container"> <div class="line number1 index0 alt2">"csharp keyword">public "csharp keyword">static "csharp keyword">string "csharp plain">HtmlSummary("csharp keyword">string "csharp plain">content, "csharp keyword">int "csharp plain">length) </div> <div class="line number2 index1 alt1">"csharp plain">{ </div> <div class="line number3 index2 alt2">"csharp spaces"> "csharp comments">//如果输入字符串长度小于截取长度,直接返回 </div> <div class="line number4 index3 alt1">"csharp spaces"> "csharp keyword">if "csharp plain">(content.Length <= length) "csharp keyword">return "csharp plain">content; </div> <div class="line number5 index4 alt2">"csharp spaces"> "csharp comments">//如果输入字符串竟未包含html代码,那就直接截取 </div> <div class="line number6 index5 alt1">"csharp spaces"> "csharp keyword">if "csharp plain">(!content.Contains("csharp string">"<""csharp plain">)) "csharp keyword">return "csharp plain">content.Substring(0, length); </div> <div class="line number7 index6 alt2">"csharp spaces"> "csharp comments">//如果输入字符串中包含手动截取标记,则返回标记前的内容 </div> <div class="line number8 index7 alt1">"csharp spaces"> "csharp keyword">if "csharp plain">(content.Contains("csharp string">"<!-- summary end -->""csharp plain">)) "csharp keyword">return "csharp plain">Regex.Split(content, "csharp string">"<!-- summary end -->""csharp plain">)[0]; </div> <div class="line number9 index8 alt2"></div> <div class="line number10 index9 alt1">"csharp spaces"> "csharp plain">var IsTag = "csharp keyword">false"csharp plain">; "csharp comments">//当前字符是否在标签中 </div> <div class="line number11 index10 alt2">"csharp spaces"> "csharp plain">var Summary = String.Empty; "csharp comments">//截取的子字符串 </div> <div class="line number12 index11 alt1">"csharp spaces"> "csharp plain">var TagName = String.Empty; "csharp comments">//当前标签名称 </div> <div class="line number13 index12 alt2">"csharp spaces"> "csharp plain">var TagArray = "csharp keyword">new "csharp plain">{ Left = "csharp keyword">new "csharp plain">ArrayList(), Right = "csharp keyword">new "csharp plain">ArrayList() }; "csharp comments">//左标签数组、右标签数组 </div> <div class="line number14 index13 alt1"></div> <div class="line number15 index14 alt2">"csharp spaces"> "csharp plain">content = FixHtml(content); </div> <div class="line number16 index15 alt1"></div> <div class="line number17 index16 alt2">"csharp spaces"> "csharp keyword">foreach "csharp plain">(var current "csharp keyword">in "csharp plain">content) </div> <div class="line number18 index17 alt1">"csharp spaces"> "csharp plain">{ </div> <div class="line number19 index18 alt2">"csharp spaces"> "csharp keyword">if "csharp plain">(length <= 0) "csharp keyword">break"csharp plain">; "csharp comments">//在截取长度>0的情况下,循环遍历html字符 </div> <div class="line number20 index19 alt1"></div> <div class="line number21 index20 alt2">"csharp spaces"> "csharp keyword">if "csharp plain">(current == "csharp string">'<'"csharp plain">) "csharp comments">//当前字符为<,进入html标签 </div> <div class="line number22 index21 alt1">"csharp spaces"> "csharp plain">{ </div> <div class="line number23 index22 alt2">"csharp spaces"> "csharp plain">IsTag = "csharp keyword">true"csharp plain">; </div> <div class="line number24 index23 alt1">"csharp spaces"> "csharp plain">} </div> <div class="line number25 index24 alt2">"csharp spaces"> "csharp keyword">else "csharp keyword">if "csharp plain">(current == "csharp string">'>'"csharp plain">) "csharp comments">//当前字符为>,移出html标签 </div> <div class="line number26 index25 alt1">"csharp spaces"> "csharp plain">{ </div> <div class="line number27 index26 alt2">"csharp spaces"> "csharp comments">//如果当前标签名称以/开头,则此标签为闭合标签,应加入右标签数组 </div> <div class="line number28 index27 alt1">"csharp spaces"> "csharp keyword">if "csharp plain">(TagName.Trim().StartsWith("csharp string">"/""csharp plain">)) </div> <div class="line number29 index28 alt2">"csharp spaces"> "csharp plain">TagArray.Right.Add(TagName); </div> <div class="line number30 index29 alt1">"csharp spaces"> "csharp comments">//否则应加入左标签数组,但应考虑自闭合标签不加入闭合标签数组 </div> <div class="line number31 index30 alt2">"csharp spaces"> "csharp keyword">else "csharp keyword">if "csharp plain">(!TagName.Trim().EndsWith("csharp string">"/""csharp plain">) && !"csharp string">"img|br|input""csharp plain">.Split("csharp string">'|'"csharp plain">).Contains(TagName.ToLower())) </div> <div class="line number32 index31 alt1">"csharp spaces"> "csharp plain">TagArray.Left.Add(TagName.Trim().ToLower().Split("csharp string">' '"csharp plain">)[0]); </div> <div class="line number33 index32 alt2"></div> <div class="line number34 index33 alt1">"csharp spaces"> "csharp plain">IsTag = "csharp keyword">false"csharp plain">; "csharp comments">//标记已移出html标签 </div> <div class="line number35 index34 alt2">"csharp spaces"> "csharp plain">TagName = String.Empty; "csharp comments">//完成标签加入标签数组后,应重置此变量 </div> <div class="line number36 index35 alt1">"csharp spaces"> "csharp plain">} </div> <div class="line number37 index36 alt2">"csharp spaces"> "csharp keyword">else</div> <div class="line number38 index37 alt1">"csharp spaces"> "csharp plain">{ </div> <div class="line number39 index38 alt2">"csharp spaces"> "csharp keyword">if "csharp plain">(IsTag) "csharp comments">//如果在html标签中,则拼积当前标签名称 </div> <div class="line number40 index39 alt1">"csharp spaces"> "csharp plain">TagName += current; </div> <div class="line number41 index40 alt2">"csharp spaces"> "csharp keyword">else "csharp comments">//否则为纯文本内容,计数器开始自减,直到减为0,循环退出 </div> <div class="line number42 index41 alt1">"csharp spaces"> "csharp plain">length--; </div> <div class="line number43 index42 alt2">"csharp spaces"> "csharp plain">} </div> <div class="line number44 index43 alt1"></div> <div class="line number45 index44 alt2">"csharp spaces"> "csharp plain">Summary += current; "csharp comments">//拼积截取的子字符串 </div> <div class="line number46 index45 alt1">"csharp spaces"> "csharp plain">} </div> <div class="line number47 index46 alt2"></div> <div class="line number48 index47 alt1">"csharp spaces"> "csharp plain">Summary = Summary.Trim(); </div> <div class="line number49 index48 alt2"></div> <div class="line number50 index49 alt1">"csharp spaces"> "csharp comments">//从左标签数组中移除已闭合的标签 </div> <div class="line number51 index50 alt2">"csharp spaces"> "csharp comments">//TagArray.Left.RemoveRange(0, TagArray.Right.Count); </div> <div class="line number52 index51 alt1">"csharp spaces"> "csharp keyword">while "csharp plain">(TagArray.Right.Count > 0) </div> <div class="line number53 index52 alt2">"csharp spaces"> "csharp plain">{ </div> <div class="line number54 index53 alt1">"csharp spaces"> "csharp plain">TagArray.Left.RemoveAt(TagArray.Left.IndexOf(TagArray.Right[0].ToString().Replace("csharp string">"/""csharp plain">,"csharp string">"""csharp plain">))); </div> <div class="line number55 index54 alt2">"csharp spaces"> "csharp plain">TagArray.Right.RemoveAt(0); </div> <div class="line number56 index55 alt1">"csharp spaces"> "csharp plain">} </div> <div class="line number57 index56 alt2"></div> <div class="line number58 index57 alt1">"csharp spaces"> "csharp comments">//剩余标签未闭合,逆序以html闭合标签格式拼接至截取的子字符串末尾 </div> <div class="line number59 index58 alt2">"csharp spaces"> "csharp keyword">if "csharp plain">(TagArray.Left.Count > 0) </div> <div class="line number60 index59 alt1">"csharp spaces"> "csharp keyword">for "csharp plain">(var i = TagArray.Left.Count - 1; i >= 0; i--) </div> <div class="line number61 index60 alt2">"csharp spaces"> "csharp plain">{ </div> <div class="line number62 index61 alt1">"csharp spaces"> "csharp plain">Summary += String.Format("csharp string">"</{0}>""csharp plain">, TagArray.Left[i].ToString()); </div> <div class="line number63 index62 alt2">"csharp spaces"> "csharp plain">} </div> <div class="line number64 index63 alt1"></div> <div class="line number65 index64 alt2">"csharp spaces"> "csharp keyword">return "csharp plain">Summary; </div> <div class="line number66 index65 alt1">"csharp plain">}</div> </div> </div>
试试其它关键字
windvoice
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3