如何使用asp.net从字符串中删除HTML标记(粗体,强,字体名称等) [英] How to remove HTML tags from string using asp.net (Bold,strong,font names etc etc)
本文介绍了如何使用asp.net从字符串中删除HTML标记(粗体,强,字体名称等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用CKEditor输入详细信息(详细信息包含html元素)。之后,细节将绑定到网格中。在这里我想在工具提示中显示没有html标签的细节。
protected void grdStepMain_RowDataBound( object sender,GridViewRowEventArgs e)
{
< span class =code-keyword> int i = 0 ;
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
i ++;
string description = cell.Text;
if (cell.Text.Length > 8 &&(i == 2 ))
cell.Text = cell.Text.Substring( 0 , 8 )+ ....跨度>;
string newdescription = Regex.Replace(description, @ < [^>] +> |& nbsp;, 跨度>)修剪();
cell.ToolTip = newdescription;
}
}
}
但它不起作用请帮助我。示例:
string str = & lt; h1& gt;& lt; span style =& quot; font-family:courier new , courier ,monospace& quot;& gt;& lt; strong& gt; 457457& lt; / strong& gt; 544444444444457457457& lt; / span& gt;;
result in tooltip 457457544444444444457457457
请帮我
问候
jithesh A
解决方案
In您的评论,您说HTML标签实际上是由HTML实体组成的。在这种情况下,从<
到>
匹配的正则表达式将不起作用;你需要一个匹配从& lt;
到& gt;
:
$ b $的正则表达式bstring newdescription = Regex.Replace(description, @ & lt;。+?& gt; |& nbsp;, 跨度>)修剪();
I am using CKEditor to enter details (The details contains html elements). after that the details will be bind into a grid. In this I want to show the details without html tags in tooltip.
protected void grdStepMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
i++;
string description = cell.Text;
if (cell.Text.Length > 8 && (i == 2))
cell.Text = cell.Text.Substring(0, 8) + "....";
string newdescription = Regex.Replace(description, @"<[^>]+>| ", "").Trim();
cell.ToolTip = newdescription;
}
}
}
But its not working please help me. Example:
string str= " <h1><span style="font-family:courier new,courier,monospace"><strong>457457</strong> 544444444444457457457</span>";
The result in tooltip should be 457457544444444444457457457
Please hep me
Regards
jithesh A
解决方案
In your comment, you said that the HTML tags are actually made up with HTML Entities. In that case, a regex that matches from<
to>
won't work; you need a regex that matches from<
to>
:
string newdescription = Regex.Replace(description, @"<.+?>| ", "").Trim();
这篇关于如何使用asp.net从字符串中删除HTML标记(粗体,强,字体名称等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文