how to limit the description text to 100-150 words using Razor
I want to display a sho开发者_如何学Pythonrter version of description on maine page , I tried something like
<div class="newsdetails">
@Html.Raw(item.Short)
</div>
Although it shorts the news description but i want to customise it to 100 words. Regards
.Have a free extension method on me. This chops strings by letters, not words. To change it to use words, consider using a method like Tobias's below.
public static string Chop(this string text, int chopLength, string postfix = "...")
{
if (text == null || text.Length < chopLength)
return text;
else
return text.Substring(0, chopLength- postfix.Length) + postfix;
}
You could use string.Split() Methode and use space as seperator.
string[] words = item.Text.Split(' ');
MSDN: http://msdn.microsoft.com/de-de/library/system.string.split%28v=vs.80%29.aspx
Afterwoods concat the words to string.
精彩评论