Thursday, October 29, 2009

Summary by word limit in .net

protected Regex _tagsRegExp = new Regex("\\s]+))?)+\\s*\\s*)/?>", RegexOptions.Compiled RegexOptions.IgnoreCase);


private string GetSummary(string text, int wordLimit)
{
try
{
//return input string directly if its empty/null.
if (!string.IsNullOrEmpty(text))
{
string inputText = _tagsRegExp.Replace(text, "").Replace(" ", " "); //gossip has case...
return getWordFromString(inputText, wordLimit);
}
else
{
return text;
}
}
catch (Exception ex) //This may catch if allowed Len is less than zero
{
ExceptionLog.Write(ex, "Unable to extract text from the string. The string len:" + text.Length + " and the word limit was : " + wordLimit);
return text;
}
}

No comments: