I want to get the first few words(100 or 200) from a long summary of words (plain string or html) using c#.
My requirement is to display the short description of the long summary of content(this content may include html elements). I'm able to retrieve the plain string but when it is html, the elements are cut it between Example, I get like this
<span style="FONT-FAMILY: Trebuchet MS">Heading</span>
</H3><span style="FONT-FAMILY: Trebuchet MS">
<font style="FONT-SIZE: 15px;
But it should return the string with full html element.
I have a Yahoo UI Editor to get the content from the user, and I'm passing that text to below method to get the short summary,
public static string GetFirstFewWords(string input, int numberWords)
{
if (input.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries).Length > numberWords)
{
// Number of words we still want to display.
int words = numberWords;
// Loop through entire summary.
for (int i = 0; i < input.Length; i++)
{
// Increment words on a space.
if (input[i] == ' ')
{
words--;
}
// If we have no more words to display, return the substring.
if (words == 0)
{
return input.Substring(0, i);
}
}
return string.Empty;
}
else
{
return input;
}
}
I'm trying this to get the article content from the user and display short summary on the list page.