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.
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)
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:
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.
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,
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
Maybe one of the uComponents extensions can help? http://ucomponents.codeplex.com/wikipage?title=Strings&referringTitle=Documentation
Jeroen
@Jeroen , I am using razor , ucomponent have an xslt version for truncate words
In Razor you can also use xslt extensions very easy. If you have uComponents installed you can probably do this in Razor:
Jeroen
Cheers Jeroen , working code:
<p>@uComponents.Core.XsltExtensions.Strings.GetFirstWords((item.description), 20,"..")</p>
is working on a reply...