Support Forums
[C#] Strip HTML from string. - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [C#] Strip HTML from string. (/showthread.php?tid=5551)



[C#] Strip HTML from string. - Yin - 04-05-2010

Code:
///<summary>
///method to strip HTML tags from a string
///</summary>
///<param name="str">the string to strip</param>
///<returns></returns>
///<remarks></remarks>
public string StripHTML(string str)
{
    try
    {
        int start = 0;
        int end = 0;
        int count = 0;

        while (((str.IndexOf("<") > -1) && (str.IndexOf(">") > -1) && (str.IndexOf("<") < str.IndexOf(">"))))
        {
            start = str.IndexOf("<");
            end = str.IndexOf(">");
            count = end - start + 1;

            str = str.Remove(start, count);
        }

        str = str.Replace(" ", " ");
        str = str.Replace(">", "");
        str = str.Replace("\r\n", "");

        return str.Trim();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

Enjoy.


RE: [C#] Strip HTML from string. - Sam - 04-05-2010

If this does what I think it does then you sir are a wise man.


RE: [C#] Strip HTML from string. - RaZoR03 - 04-18-2010

(04-05-2010, 05:43 PM)Yin Wrote:
Code:
///<summary>
///method to strip HTML tags from a string
///</summary>
///<param name="str">the string to strip</param>
///<returns></returns>
///<remarks></remarks>
public string StripHTML(string str)
{
    try
    {
        int start = 0;
        int end = 0;
        int count = 0;

        while (((str.IndexOf("<") > -1) && (str.IndexOf(">") > -1) && (str.IndexOf("<") < str.IndexOf(">"))))
        {
            start = str.IndexOf("<");
            end = str.IndexOf(">");
            count = end - start + 1;

            str = str.Remove(start, count);
        }

        str = str.Replace(" ", " ");
        str = str.Replace(">", "");
        str = str.Replace("\r\n", "");

        return str.Trim();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

Enjoy.

Nice dude,thanks