Problems Truncating String

Results 1 to 2 of 2

Thread: Problems Truncating String

  1. #1
    Join Date
    Dec 1969

    Default Problems Truncating String

    I have been using this code, which I think I got from here actually:<BR>Function TruncateString(LongStr, TruncateLength)<BR>Dim tmp, count<BR>TruncateString = left(LongStr,TruncateLength)<BR>TruncateString = StripHTML(TruncateString)<BR>&#039;LongSTR = striphtml(LongStr)<BR>TruncateString = Replace(TruncateString, "<BR>", " ")<BR>TruncateString = Replace(TruncateString, "<BR>", " ")<BR>TruncateString = Replace(TruncateString, "&#060;P&#062;", " ")<BR>TruncateString = Replace(TruncateString, "&#060;p&#062;", " ")<BR>tmp=TruncateString<BR>count=1<BR>do while right(TruncateString,1) &#060;&#062; chr(32)<BR> TruncateString = left(tmp,TruncateLength-count) & "... "<BR>count=count+1<BR>loop<BR>End Function<BR><BR>And for the most part it works fine, but when I pass it a large volume of text, it doesn&#039;t truncate it at the closest space like it is supposed to, it truncates it in the middle of the word. Anyone have any ideas? Thanks!<BR>Scott.<BR>

  2. #2
    Join Date
    Dec 1969

    Default Sure HOPE you didn't...

    ...get that here! <BR><BR>Pardon me, but YUCK!<BR><BR>(1) What happens if the user has &LT;Br&#062; or &LT;bR&#062; instead of just the &LT;br&#062; and &LT;BR&#062; that you test for?<BR>(2) Never heard of InStrRev function? Or the MID function? That loop that chops off characters is about as inefficient as you could get, since it creates a whole new string every time through the loop!<BR>(3) You don&#039;t bother to show the code for the StripHTML function. May I bet that it *already* strips out the &LT;BR&#062; and &LT;P&#062; tags, so you are duplicating effort?<BR>(4) The code keeps assigning to the function-result variable. This works, but it&#039;s slower than using a local variable until ready to return the final result.<BR>(5) You are truncating the string to the given length *before* doing the transformations. This is more efficient, but it certainly could end up producing a final string that is much shorter than the given length.<BR><BR>As for why it mucks up... Come on! The variable TruncateLength is the length of the *ORIGINAL* string, before all your transformations! So when you do Left(tmp,TruncateLength-count) you are completely screwed up by using the wrong basis for the length of the tmp string!<BR><BR>In short...toss that code and start again. This is *not* advanced code. This kind of question has been answered many, many times in the Q&A forum.<BR><BR>Anyway...<BR><BR>I left in the call to stripHTML even though I haven&#039;t a clue as to what it is actually doing.<BR><BR>&LT;%<BR>Function TruncateString(LongStr, TruncateLength)<BR>&nbsp; &nbsp; Dim tmp, spaceAt<BR>&nbsp; &nbsp; &#039; should you REALLY be doing this here???<BR>&nbsp; &nbsp; tmp = left(LongStr,TruncateLength)<BR>&nbsp; &nbsp; tmp = StripHTML(tmp)<BR>&nbsp; &nbsp; &#039; handle upper and lower case and mixed<BR>&nbsp; &nbsp; tmp = Replace(tmp,"&LT;BR&#062;"," ",1,-1,vbTextCompare)<BR>&nbsp; &nbsp; tmp = Replace(tmp,"&LT;P&#062;"," ",1,-1,vbTextCompare)<BR>&nbsp; &nbsp; spaceAt = InStrRev( tmp, " " ) &#039; find last space<BR>&nbsp; &nbsp; &#039; if we don&#039;t find a space in last 40 characters, don&#039;t chop at space<BR>&nbsp; &nbsp; If spaceAt &#062; 0 AND spaceAt &#062;= ( Len(tmp) - 40 ) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; tmp = Left(tmp,spaceAt-1)<BR>&nbsp; &nbsp; End If<BR>&nbsp; &nbsp; TruncateString = tmp<BR>End Function<BR>%&#062;<BR><BR>You can modify that "spaceAt &#062;= ( Len(tmp) - 40 )" to whatever heuristic you want, of course.<BR><BR> <BR> <BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts