Strip out all html tags in string using C#

The tags need not be correctly nested for the below implementation to work..

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

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.