Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Classic ASP - Text Truncate Function

Featured Replies

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.

  • Author

Well, is there any other method other than the classic asp or html/css solution to achieve this?

<%
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

  • 1 year later...

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.