Hi bruce,
You can use the following method as:
htmlcontent variable contain html content as well as normal content.
string stringcontent = TrimNewLines(RemoveDoubleNewLines(RemoveAllTags(htmlcontent)));
if(stringcontent.Length > 300) { stringcontent = stringcontent.Substring(0, 300)+" ..."; }
/// <summary>
/// Replaces every tag with new line
/// </summary>
private static string RemoveAllTags(string str)
{
string strWithoutTags =
Regex.Replace(str, "<[^>]*>", "\n");
return strWithoutTags;
}
/// <summary>
/// Replaces sequence of new lines with only one new line
/// </summary>
private static string RemoveDoubleNewLines(string str)
{
string pattern = "[\n]+";
return Regex.Replace(str, pattern, "\n");
}
/// <summary>
/// Removes new lines from start and end of string
/// </summary>
private static string TrimNewLines(string str)
{
int start = 0;
while (start < str.Length && str[start] == '\n')
{
start++;
}
int end = str.Length - 1;
while (end >= 0 && str[end] == '\n')
{
end--;
}
if (start > end)
{
return string.Empty;
}
string trimmed = str.Substring(start, end - start + 1);
return trimmed;
}
Posted On:
30-May-2015 16:07