Published on: 12 Oct, 2019

How to remove html tags from string in c#?

Tumblr Digg Google Plus

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database. Or alternatively, you just want to store complete clean string text only.

To clean it up in C# we can use a regular expression. We can use the following method to remove unneeded HTML tags from a string.

public static string RemoveHTMLTags(string value)
{
	Regex regex = new Regex("\\<[^\\>]*\\>");
	value = regex.Replace(value, String.Empty);
	return value;
}

Below is an example on how to use it.

string cleanText = RemoveHTMLTags("<p>This is a paragrah text.</p>");

The result of the clean up will be without the p tags.

Below is an example on how to use it.

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.