InstantASP Community Forums
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        


Optimized the Truncate function Expand / Collapse
Author
Message
Posted 08/09/2006 08:05:13


Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Customers
Last Login: 26/03/2007 05:11:10
Posts: 46, Visits: 152
In my personal optimization crusade I found the function Truncate called many times and slow.

Here is a way to fully remove the double for loop, multiple array, split and concatenation issue. Simple and working.

    Public Shared Function Truncate(ByVal Input As String, ByVal intMaxLen As Int32) As String

      If Input Is Nothing Then
        Return String.Empty
      End If

      If Input.Length <= intMaxLen Then
        ' no need to truncate
        Return Input
      End If

      ' search for last space before max lenght
      Dim iCutAt As Integer = Input.LastIndexOf(" "c, intMaxLen)

      Dim inputCut As String
      If iCutAt = -1 Then
        ' there is no space so cut anywhere
        inputCut = Input.Substring(0, intMaxLen)
      Else
        inputCut = Input.Substring(0, iCutAt)
      End If

      Return inputCut + "..."

End Function

I haven't done any benchmark but it's probably 100 times faster for both long and short input string.

Running InstantASP forum with many code change.

Post #11711
« Prev Topic | Next Topic »

Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Ryan Healey, Mark Christianson, Bare, James Trott

Permissions Expand / Collapse

All times are GMT, Time now is 1:30pm

Powered by InstantForum.NET v4.1.4 © 2008
Execution: 0.031. 9 queries. Compression Disabled.
Home | Products | Purchase | Support | Company | Contact Us
Privacy Statement | © 1999-2007 InstantASP Limited. All Rights Reserved.