Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • saintwright 69 posts 77 karma points
    Jun 27, 2011 @ 13:10
    saintwright
    0

    Substring

    Seems like this should be easy - how do I get a substring of say  @item.BodyText in a template file?


    Just want to display a snippet..

     

    thanks



  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 27, 2011 @ 17:08
    Dan Diplo
    0

    If, say, you wanted just the first 150 characters you would do:

    @Html.Raw(Model.BodyText.ToString().Substring(0, 150)); 

    Explanation: Model.BodyText.ToString() gives you the raw HTML and then from there you can use any standard String method, such as Substring(). The Html.Raw razor helper method is used to tell Razor NOT to HTML Encode the output.

    Just note you always risk splitting in the middle of an HTML tag using this method. A better way is to strip out HTML first and then perform a substring. There are umbraco.library helpers to do this. 

  • saintwright 69 posts 77 karma points
    Jun 29, 2011 @ 01:13
    saintwright
    0

    thank you

Please Sign in or register to post replies

Write your reply to:

Draft