Search code examples
c#asp.net-mvcrazorumbraco

umbraco string trim using razor view


I have a Umbraco script Im using on a site, inside it there is a razor script as below:

<p>@page.GetProperty("mainContent")</p>

The above is in a loop, and shows content for each post (its being used on a landing page with blog like functionality)

I want to trim the content outputed by the GetPropery() method to say 300 charectors.

Anyone have any ideas?

Also, what word is the opposite of concatenate?


Solution

  • You could write a custom helper:

    public static class HtmlExtensions
    {
        public static string Truncate(this HtmlHelper html, string value, int count)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
    
            if (value.Length > count)
            {
                value = value.Substring(0, count - 1) + "...";
            }
    
            return value;
        }
    }
    

    which could be used like this:

    <p>@Html.Truncate(page.GetProperty("mainContent"), 300)</p>
    

    Also, what word is the opposite of concatenate?

    Split