Strip out HTML tags using RegEx
13 Nov 2007This code will strip out all the HTML tags and truncate the text to 4 lines.
public static string TruncateText(string txtIn, int newLength) { string txtOut = txtIn; string pattern = @"<(.|\n)*?>"; //Strip out HTML tags if (Regex.IsMatch(txtIn, pattern, RegexOptions.None)) txtOut = Regex.Replace(txtIn, pattern, string.Empty, RegexOptions.Multiline).Trim(); if (txtOut.Length > newLength) { int endPos = txtOut.LastIndexOf(" ", newLength); txtOut = txtOut.Substring(0, endPos) + "..."; } return txtOut; }