Copied to clipboard

Flag this post as spam?

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


  • Mr A 216 posts 278 karma points
    Jul 25, 2012 @ 01:24
    Mr A
    0

    Truncate HTML to finish at the end of a word using razor

    I believe this can be done, but can't find anything to work for me.

    I want my truncated HTML to finish at the end of a word instead of halfway into a word. I think it would work by first calculating the number of characters, then finishing at a space " " instead of the last character.

    <p>@(Library.Truncate(Library.StripHtml(item.newsShort), 40true))</p>
    

    So in the above example it would display the first 40 HTML characters from mainContent, and finish on a space, or before a comma, period, hyphen or any other nominated characters.

    So instead of :
    We will build the new $13m Sport, Rec... (40 characters)

    It would be:
    We will build the new $13m Sport... (35 characters)

    Has anyone got a solution to this?

    Cheers,

  • Paul Blair 466 posts 731 karma points
    Jul 25, 2012 @ 03:31
    Paul Blair
    0

    Instead of using the truncate method you will need to write your own method. As you're using Razor anyway it should be pretty simple to add your own helper method based on something like this:

    http://stackoverflow.com/questions/1613896/truncate-string-on-whole-words-in-net-c-sharp

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 25, 2012 @ 10:05
    Jeroen Breuer
    0

    Maybe one of the uComponents extensions can help? http://ucomponents.codeplex.com/wikipage?title=Strings&referringTitle=Documentation

    Jeroen

  • Mr A 216 posts 278 karma points
    Jul 25, 2012 @ 10:54
    Mr A
    0

    @Jeroen , I am using razor , ucomponent have an xslt version for truncate words

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 25, 2012 @ 11:06
    Jeroen Breuer
    1

    In Razor you can also use xslt extensions very easy. If you have uComponents installed you can probably do this in Razor:

    @{
    string s = uComponents.Core.XsltExtensions.Strings.GetFirstWords("this is a test", 3);

    Jeroen

  • Mr A 216 posts 278 karma points
    Jul 25, 2012 @ 11:31
    Mr A
    0

    Cheers Jeroen , working code:

    <p>@uComponents.Core.XsltExtensions.Strings.GetFirstWords((item.description), 20,"..")</p>

Please Sign in or register to post replies

Write your reply to:

Draft