Laith Zraikat

I Innovate, Therefore I Am.

Strip HTML tags from a string in .NET

The best and shortest way is to use Regular Expression for this task:

VB.Net

Function StripHTMLTags(ByVal HTMLToStrip AsString) AsString
Dim stripped AsString
If HTMLToStrip <> "" Then
stripped = Regex.Replace(HTMLToStrip, "<(.|\n)+?>", String.Empty)
Return stripped
Else
Return ""
EndIf
EndFunction


C#

privatestring StripHTMLTags(string HTMLToStrip)
{
string stripped;
if (!Convert.ToBoolean(HTMLToStrip == ""))
{
stripped = Regex.Replace(formHTML.Text, @"<(.|\n)*?>", string.Empty);
return stripped;
}
else
{
return"";
}
}







Add a Comment

Rajkumar
December, 19, 2005 9:06 AM
This is really handy. But this also strips a simple text like <abc>