In this tutorial i am going to explain about how to strip/remove html tags from the string using regular expression in asp.net and C#.Net or It is achieved by using the System.Text.RegularExpressions namespace.
In my previous article i have explained about Sort Dictionary Based On Value In Asp.Net , Get All Coutries From System.Globalization Namespace , Web Api Model Validation Using Validation Filters , Convert String To Upper,Lower & Title(Proper) Case Using TextInfo Class Build Products Comparision Table/Grid In ASP.Net , Store Custom Objects In Configuration File and many articles in ASP.Net,C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.
In this tutorial for explaining purpose i have a textbox to get the html string as input and a button control. While clicking on the button it will strip the html and output the plain text in the next textbox. Below i have mentioned the entire code.
HTML Markup:
Below is the cs code.
C#.Net:
VB.Net:
If you run the application then you will get the below output.
SourceCode:
If you like this article then share with your friends and comment your valuable feedback.. Happy coding..
In my previous article i have explained about Sort Dictionary Based On Value In Asp.Net , Get All Coutries From System.Globalization Namespace , Web Api Model Validation Using Validation Filters , Convert String To Upper,Lower & Title(Proper) Case Using TextInfo Class Build Products Comparision Table/Grid In ASP.Net , Store Custom Objects In Configuration File and many articles in ASP.Net,C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.
In this tutorial for explaining purpose i have a textbox to get the html string as input and a button control. While clicking on the button it will strip the html and output the plain text in the next textbox. Below i have mentioned the entire code.
HTML Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Regular Expression Code to
Remove HTML tags in C# </h2>
<table>
<tr>
<td>Input Html</td>
<td>
<asp:TextBox ID="txtHtml" runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnStripHtml" runat="server" OnClick="btnStripHtml_Click"
Text="Strip Html" />
</td>
</tr>
<tr>
<td>Plain Text</td>
<td>
<asp:TextBox ID="txtPlainText" runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Below is the cs code.
C#.Net:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
// Include this if it is not already there
using
System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Method to strip HTML Tags
public string StripHtmlTags(string source)
{
return Regex.Replace(source, "<.*?>|&.*?;", string.Empty);
}
protected void btnStripHtml_Click(object sender, EventArgs e)
{
// Getting Input HTML
string inputHtml =
txtHtml.Text;
// Removing HTML tags including from the input
string outputText =
StripHtmlTags(inputHtml);
//
Assigning plain text output to output textbox
txtPlainText.Text = outputText;
}
}
VB.Net:
Imports
System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports
System.Web.UI.WebControls
' Include this if it is not already there
Imports
System.Text.RegularExpressions
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
' Method to strip HTML Tags
Public Function
StripHtmlTags(source As String) As String
Return Regex.Replace(source, "<.*?>|&.*?;", String.Empty)
End Function
Protected Sub btnStripHtml_Click(sender As Object, e As EventArgs)
' Getting Input HTML
Dim inputHtml As String = txtHtml.Text
' Removing HTML tags including from the input
Dim outputText As String =
StripHtmlTags(inputHtml)
' Assigning plain text output to output textbox
txtPlainText.Text = outputText
End Sub
End Class
If you run the application then you will get the below output.
SourceCode:
If you like this article then share with your friends and comment your valuable feedback.. Happy coding..
Comments
Post a Comment