0

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/&gt; 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?

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
user123456789
  • 1,914
  • 7
  • 44
  • 100
  • 2
    [Obligatory link](http://stackoverflow.com/a/1732454/2307070) about why ṫ̨̗̺̭̮̞̗̜̮̗̙̫̺̖̭̯͊ͨ̌͒̍͘͘͟͝h̸͓̩̙͙̻̗͔̞̘̟̩̯͋͑͂͐a̴̧ͨ́ͭ͒ͯ̓͐̇̃ͥ͢҉‌​̨̳̜̤͍͖t̵̳̳͕͉͋̓͐ͦͬ̈́̀̚‌ is a bad idea. Also, are you sure it's `;br/>` and not `<br/>`? – Thomas Ayoub Apr 11 '17 at 12:03
  • @ThomasAyoub yes you are right it is `<br/>`. So if I don't use regex then what should I use? – user123456789 Apr 11 '17 at 12:08

3 Answers3

4

Since this is a literal string, you (sh|c)ould only use String.Replace():

static string RemoveHTMLNewLines(string text)
{
    return text.Replace("&lt;br/&gt;", string.Empty);
}

Or replace with Environment.NewLine if needed.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
2
  1. De-entitize the string.
  2. Then use regex to to find and remove expected tags.

Or

If you have enough time to study and Use, then use HtmlAgilityPack package.

About HtmlAgilityPack

Nuget Package Link

ravindra
  • 322
  • 3
  • 14
1

Being regardless of the question, I am curious about what this is:

enter image description here

Karr
  • 395
  • 5
  • 9