Click here to Skip to main content
15,910,586 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m creating content management in asp .In sql database contains table in that HTML tag and website content data is stored. I want to view website content from sql table to text box of asp but i don't want to view HTML tag in text box of asp. please help me i m new in .net
Thanks and regards
Posted

1 solution

using System;
using System.Text.RegularExpressions;

/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
    /// <summary>
    /// Remove HTML from string with Regex.
    /// </summary>
    public static string StripTagsRegex(string source)
    {
	return Regex.Replace(source, "<.*?>", string.Empty);
    }

    /// <summary>
    /// Compiled regular expression for performance.
    /// </summary>
    static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);

    /// <summary>
    /// Remove HTML from string with compiled Regex.
    /// </summary>
    public static string StripTagsRegexCompiled(string source)
    {
	return _htmlRegex.Replace(source, string.Empty);
    }

    /// <summary>
    /// Remove HTML tags from string using char array.
    /// </summary>
    public static string StripTagsCharArray(string source)
    {
	char[] array = new char[source.Length];
	int arrayIndex = 0;
	bool inside = false;

	for (int i = 0; i < source.Length; i++)
	{
	    char let = source[i];
	    if (let == '<')
	    {
		inside = true;
		continue;
	    }
	    if (let == '>')
	    {
		inside = false;
		continue;
	    }
	    if (!inside)
	    {
		array[arrayIndex] = let;
		arrayIndex++;
	    }
	}
	return new string(array, 0, arrayIndex);
    }
}


using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
	const string html = "<p>There was a .NET programmer " +
	    "and he stripped the HTML tags.</p>";

	Console.WriteLine(HtmlRemoval.StripTagsRegex(html));
	Console.WriteLine(HtmlRemoval.StripTagsRegexCompiled(html));
	Console.WriteLine(HtmlRemoval.StripTagsCharArray(html));
    }
}


This gives
XML
<p>There was a <b>.NET</b> programmer " +
        "and he stripped the <i>HTML</i> tags.</p>


There was a .NET programmer and he stripped the HTML tags.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900