Remove HTML tags in String in C#

If the string is like that

string s = “"<p>hello <b>there</b></p>"

 

Use the regular expression:

 

using System.Text.RegularExpressions;
...

static string StripHTML (string inputString)
{
   return Regex.Replace 
     (inputString, "<.*?>", string.Empty);
}


But if the input string is like this:

String s = "&lt;/b.......&gt;"

You need to decode it first using http utils as follows:

Add a sub functiona and pass the input string to that to decode the string and use the regular expression avobe.

public static string DecodeHtmlEntities(string text)
    {
        return HttpUtility.HtmlDecode(text);
    }


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s