/ Published in: ASP
Expand |
Embed | Plain Text
Function shortenText(text, length, append) if (Len(text) > length) then 'shorten if greater than length descriptionArr = Split(text, " ") 'split words into an array text = "" for i = 0 to UBound(descriptionArr) descriptionWord = descriptionArr(i) if (Len(descriptionWord) + Len(text) + 1 <= length) then 'if new word plus sentence being put together is shorten than length' text = text & descriptionWord & " " else exit for end if next if append <> "" then text = text & append end if shortenText = text else shortenText = text end if End Function
Comments
Subscribe to comments
You need to login to post a comment.

BEWARE of using this on anything other than short sentences - string concatenation in a loop (line 8) is VERY inefficient and will quickly lead to significant performance problems (such as server CPU hogging) when processing long inputs with this function!
See my article at ASP101 on this subject for more information on this issue (including alternatives).
Yes, very good point. Thank you for pointing to your article. I have been using this to pull a very shortened description from an RSS feed.