C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 300,504 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,904 people online right now. Registration is fast and FREE... Join Now!





Strip HTML from string using Regular Expressions

This is a snippet I have used often to strip all HTML characters from a string before putting the data into the database

Submitted By: PsychoCoder
Actions:
Rating:
Views: 7,406

Language: C#

Last Modified: December 31, 2007
Instructions: Pass the string you would like stripped to the method, accept the return value. Need reference to System.Text.RegularExpressions Namespace

Snippet


  1. //namespace reference
  2. using System.Text.RegularExpressions;
  3.  
  4. /// <summary>
  5. /// method to strip HTML tags using Regular Expressions
  6. /// </summary>
  7. /// <param name="str">String to strip HTML from</param>
  8. /// <returns></returns>
  9. public string StripHTML(string str)
  10. {
  11.     //variable to hold the returned value
  12.     string strippedString;
  13.     try
  14.     {
  15.         //variable to hold our RegularExpression pattern
  16.         string pattern = "<.*?>";
  17.         //replace all HTML tags
  18.         strippedString = Regex.Replace(str, pattern, string.Empty);
  19.     }
  20.     catch
  21.     {
  22.         strippedString = string.Empty;
  23.     }
  24.     return strippedString;
  25. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month