IMG-LOGO

How to remove html tags from string in c#?

andy - 12 Oct, 2019 15766 Views 1 Comment

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.

Related Articles

Free Open Source .Net CMS

Are you looking for an open source .Net CMS for your site? When we say open source it means you can get full access to the source code without having to pay a cent.