Support Forums

Full Version: [C#] Strip HTML from string.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
If this does what I think it does then you sir are a wise man.
(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