首页 新闻 会员 周边

C# 问题 有html标签的数据 只截取某个长度的字符串 有什么好的解决方案吗

0
悬赏园豆:20 [已解决问题] 解决于 2015-10-10 15:57

  例子(数据):

           <p style="margin: 23px auto 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 26px; font-family: SimSun; overflow: visible !important; color: rgb(43, 43, 43); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;">10月5日,<a href="http://country.huanqiu.com/america" class="linkAbout" target="_blank" title="美国" style="color: rgb(6, 52, 111); text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(6, 52, 111); margin: 0px 5px; padding: 0px 0px 2px; font-size: 14px;">美国</a>等12国经贸部长发表联合声明,宣布《跨太平洋伙伴关系协定》(以下简称TPP)谈判结束,引发境内外舆论高度关注和热议。日前,商务部高虎城部长就社会关注的TPP热点问题接受中央主流媒体采访。</p><p style="margin: 23px auto 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 26px; font-family: SimSun; overflow: visible !important; color: rgb(43, 43, 43); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;">  <strong style="font-size: 14px; font-family: SimSun;">问: 10月5日,美国等12国经贸部长发表联合声明,宣布历时5年多的TPP协定谈判结束。中方如何评价?</strong></p><p style="margin: 23px auto 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 26px; font-family: SimSun; overflow: visible !important; color: rgb(43, 43, 43); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;">  答:中方注意到,美国等12国经贸部长于当地时间10月5日在美国亚特兰大宣布TPP谈判结束。中方一贯认为并多次强调,亚太地区已经完成的和正在进行谈判的自贸协定,包括TPP,均是为促进亚太地区贸易投资自由化和便利化、推进区域一体化、推动经济发展的重要路径,是为促进世界经济可持续发展做出的贡献。中方对符合<a href="http://country.huanqiu.com/wto" class="linkAbout" target="_blank" title="世界贸易组织" style="color: rgb(6, 52, 111); text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(6, 52, 111); margin: 0px 5px; padding: 0px 0px 2px; font-size: 14px;">世界贸易组织</a>规则、有助于促进亚太区域经济一体化的制度建设均持开放态度。中方希望TPP与本地区其他自由贸易安排相互促进,共同为亚太地区的贸易投资和经济发展做出贡献。</p><p><br/></p>

崔东生的主页 崔东生 | 初学一级 | 园豆:131
提问于:2015-10-09 15:43
< >
分享
最佳答案
0

html是你的内容

/// <summary> 过滤HTML标签 替换所有小于号,并给符合条件的转换回来 </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string Filter(string html)
{
html = html.Replace("<", "&lt;");
html = Regex.Replace(html, "&lt;(?=(span|[/]span|p|[/]p|strong|[/]strong|em|[/]em|/br|br/))", "<");
return html;
}

试试

 

Regex.Replace(html, "[<].*?[>]", "");

直接替换所有

收获园豆:10
晓菜鸟 | 老鸟四级 |园豆:2594 | 2015-10-09 15:46
其他回答(3)
0

直接正则去掉<>之间的内容即可!

Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

收获园豆:10
at小怪兽 | 园豆:760 (小虾三级) | 2015-10-09 15:46
0

string.substring

请叫我头头哥 | 园豆:9382 (大侠五级) | 2015-10-09 15:55
0

用正则表达式 截取。

唯美菠萝 | 园豆:282 (菜鸟二级) | 2015-10-10 10:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册