[thelist] ASP newbie with needs help asking a question

Ken Schaefer ken at adOpenStatic.com
Thu Mar 18 21:12:09 CST 2004


I think that function has a slight flaw. It appears that if the parameter
"str_val" has spaces, but the first space is after "char_limit" then only
"..." will be returned.

I think the following will work, and it avoids concatenating the output
string in a loop.

<%
Response.Write(TruncateString(strTitle, intCharLimit))

Function TruncateString( _
    ByVal strTitle, _
    ByVal intCharLimit _
    ) ' As String

    Dim strOutput
    Dim intLastSpacePosition

    strOutput = strTitle

    ' Only do something if strOutput > intCharLimit
    If Len(strTitle) > intCharLimit then

        ' Locate the last space before intCharLimit
        intLastSpacePosition = InStrRev(strTitle, " ", intCharLimit, 1)

        If intLastSpacePosition = 0 then

            ' If no spaces, then truncate at intCharLimit
            strOutput = Left(strTitle, intCharLimit)

        Else

            ' Truncate at last space
            strOutput = Left(strTitle, intLastSpacePosition)

        End If

        strOutput = strOutput & "..."

    End If

    ' Return output
    TruncateString = strOutput

End Function
%>

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Maximillian Schwanekamp" <anaxamaxan at neptunewebworks.com>
Subject: RE: [thelist] ASP newbie with needs help asking a question


: For what it's worth, a little function to use if you wanted to break at a
: word, count backward from your upper character limit:
:
: response.write truncateString(some_string,50)
:
: private function truncateString(str_val,char_limit)
: dim tmpstr,tmparr, i
: if len(str_val)<=char_limit then
: 'no need to truncate
: truncateString = str_val
: exit function
: else
: str_val=left(str_val,char_limit)
: tmparr=split(str_val," ")
: if instr(str_val," ")=0 then
: 'all one word, just chop it off
: tmpstr = left(str_val,char_limit)
: else
: for i = 0 to ubound(tmparr)
: if len(tmpstr & " " & tmparr(i))>char_limit then
: exit for
: else
: tmpstr = tmpstr & " " & tmparr(i)
: end if
: next
: end if
: tmpstr = tmpstr & "..."
: end if
: truncateString = tmpstr
: end function
:
: Good ASP resources:
: http://www.devguru/ - Free quick references for VBScript (most Classic ASP
: is in VBScript) and ASP (the ASP-specific objects).  You can buy
: downloadable versions that are invaluable for, er, quick referencing...
: http://www.4guysfromrolla.com - you'll find links to zillions of other ASP
: resources from there.
:
: Maximillian Von Schwanekamp
:
:
: -- 
: * * Please support the community that supports you.  * *
: http://evolt.org/help_support_evolt/
:
: For unsubscribe and other options, including the Tip Harvester
: and archives of thelist go to: http://lists.evolt.org
: Workers of the Web, evolt !
:



More information about the thelist mailing list