Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Strip HTML tags from a string

From Wiki

Jump to: navigation, search

Summary: Strip HTML Tags from a string

If you have a string containing HTML and you need to strip all of the HTML out of it, one of the easiest methods is to use a Regular Expression.

Here's a simple example that shows how to use this expression:

  1. Imports System.Text.RegularExpressions
  2. Partial Class Default1
  3.     Inherits System.Web.UI.Page
  4.  
  5.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.         Dim myHTML As String = "<html><head></head><body style=""color:blue;"">this is some text</body></html>"
  7.         Response.Write(ConvertHtmlToPlainText(myHTML))
  8.     End Sub
  9.  
  10.     Private Function ConvertHtmlToPlainText(ByVal htmlText As String) As String
  11.         Return Regex.Replace(htmlText, "<[^>]*>", String.Empty)
  12.     End Function
  13. End Class

Ouptut:

  1. this is some text


This Hack is part of the ASP.NET Hacks collection

481 Rating: 3.3/5 (12 votes cast)