Sometimes we need to truncate a string if it is longer than a particular number of characters - for example when putting an entry into a navigation bar or other application where there is limited space.
Umbraco is very XSLT orientated and our navigation menu uses it to dynamically generate it's items. We found the need to truncate the titles of these items to keep the menu looking tidy. Here's how:
<!-- if YOUR_STRING is greater than the MAXIMUM_LENGTH
<xsl:when test="string-length( YOUR_STRING ) > MAXIMUM_LENGTH ">
<!-- print out the truncated value followed by "..."
<xsl:value-of select="substring( YOUR_STRING ,0, MAXIMUM_LENGTH )"/>...
<!-- otherwise print out the whole, un-truncated string -->
<xsl:value-of select=" YOUR_STRING "/>
Remember to replace YOUR_STRING with your string's name and MAXIMUM_LENGTH with a whole number, e.g. 8 or 113