Reputation: 2004
I am reading in a field from the database and displaying in a GridView and in that field it contains <br/>
tags in the text. So I am trying to remove these from the code but when I check the value of e.Row.Cells[index].Text
it doesn't contain <br/>
and is has ;br/>
instead.
So I tried creating a function that removes any substring starting with <
and ending with >
or starting with &
and ending with ;
. The code removes the <>
but it is still showing br/
Code:
index = gv.Columns.HeaderIndex("Message");
if (index > 0)
{
string message = RemoveHTMLMarkup(e.Row.Cells[index].Text);
e.Row.Cells[index].Text = message;
}
static string RemoveHTMLMarkup(string text)
{
return Regex.Replace(Regex.Replace(text, "<.+?>", string.Empty), "&.+?;", string.Empty);
}
How do I remove the <br/>
tag?
Upvotes: 0
Views: 1515
Reputation: 29431
Since this is a literal string, you (sh|c)ould only use String.Replace()
:
static string RemoveHTMLNewLines(string text)
{
return text.Replace("<br/>", string.Empty);
}
Or replace with Environment.NewLine
if needed.
Upvotes: 4
Reputation: 357
Or
If you have enough time to study and Use, then use HtmlAgilityPack package.
Upvotes: 2