August 11, 20242 yr Hi, anybody here knows the simplest Classic ASP function to truncate a long text? Example: 1. This is a very long text. 2. This is a very... Text got truncated at certail length.
August 15, 20242 yr Author Well, is there any other method other than the classic asp or html/css solution to achieve this?
August 15, 20242 yr <% Function TruncateStringWithoutBreakingWords(str, maxLength) If Len(str) <= maxLength Then TruncateStringWithoutBreakingWords = str Else ' Find the position of the last space within the allowed length Dim truncatedStr truncatedStr = Left(str, maxLength) Dim lastSpacePosition lastSpacePosition = InStrRev(truncatedStr, " ") ' If there's a space within the limit, cut at the last space to avoid breaking a word If lastSpacePosition > 0 Then truncatedStr = Left(truncatedStr, lastSpacePosition - 1) End If ' Add ellipsis TruncateStringWithoutBreakingWords = truncatedStr & "..." End If End Function ' Example usage: Dim originalString originalString = "This is a long string that needs truncating without breaking words." Response.Write(TruncateStringWithoutBreakingWords(originalString, 25)) ' Output: "This is a long string..." %> Took me less than a minute to find this with a google search
December 18, 2025Dec 18 Yes In Classic ASP (VBScript), the simplest way is to use the built-in Left() function. Example: <% Function TruncateText(txt, maxLength) If Len(txt) > maxLength Then TruncateText = Left(txt, maxLength) & "..." Else TruncateText = txt End If End Function Response.Write TruncateText("This is a very long text.", 15) %> Output: This is a very... This checks the text length, cuts it at the desired character limit, and adds … only when needed.
Create an account or sign in to comment