﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>InstantASP Community Forums / Old Forums / InstantForum.NET 4.x / Suggestions &amp; Requests  / Optimized the Truncate function / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>InstantASP Community Forums</description><link>http://community.instantasp.co.uk/</link><webMaster>sales@instantasp.co.uk</webMaster><lastBuildDate>Fri, 09 Jan 2009 04:07:59 GMT</lastBuildDate><ttl>20</ttl><item><title>Optimized the Truncate function</title><link>http://community.instantasp.co.uk/Topic11711-51-1.aspx</link><description>In my personal optimization crusade I found the function Truncate called many times and slow.&lt;P&gt;Here is a way to fully remove the double for loop, multiple array, split and concatenation issue. Simple and working.&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;    Public Shared Function Truncate(ByVal Input As String, ByVal intMaxLen As Int32) As String&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;      If Input Is Nothing Then&lt;BR&gt;        Return String.Empty&lt;BR&gt;      End If&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;      If Input.Length &amp;lt;= intMaxLen Then&lt;BR&gt;        ' no need to truncate&lt;BR&gt;        Return Input&lt;BR&gt;      End If&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;      ' search for last space before max lenght&lt;BR&gt;      Dim iCutAt As Integer = Input.LastIndexOf(" "c, intMaxLen)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;      Dim inputCut As String&lt;BR&gt;      If iCutAt = -1 Then&lt;BR&gt;        ' there is no space so cut anywhere&lt;BR&gt;        inputCut = Input.Substring(0, intMaxLen)&lt;BR&gt;      Else&lt;BR&gt;        inputCut = Input.Substring(0, iCutAt)&lt;BR&gt;      End If&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;      Return inputCut + "..."&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" color=#5555dd&gt;End Function&lt;/FONT&gt;&lt;P&gt;&lt;FONT face=Arial color=#111111&gt;I haven't done any benchmark but it's probably 100 times faster for both long and short input string.&lt;/FONT&gt;</description><pubDate>Fri, 08 Sep 2006 08:05:13 GMT</pubDate><dc:creator>Zyo</dc:creator></item></channel></rss>