Mega Code Archive

 
Categories / C# Tutorial / Regular Expression
 

Strip HTML start and end tags from each string if they are present

using System; public class Example  {    public static void Main()     {       string[] strSource = { "<b>bold</b>", "<H1>header</H1>"};       foreach (string s in strSource)       {          string item = s;          if (item.Trim().EndsWith(">"))           {             int endTagStartPosition = item.LastIndexOf("</");             if (endTagStartPosition >= 0 )                item = item.Substring(0, endTagStartPosition);             if (item.Trim().StartsWith("<"))             {                int openTagEndPosition = item.IndexOf(">");                if (openTagEndPosition >= 0)                   item = item.Substring(openTagEndPosition + 1);             }                }          Console.WriteLine(item);       }                       } }