开发者

how to limit the description text to 100-150 words using Razor

开发者 https://www.devze.com 2023-04-08 11:49 出处:网络
I want to display a sho开发者_如何学Pythonrter version of description on maine page , I tried something like

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号