Classic ASP Trim function

Hi guys

I sincerely hope you can help. In my classic ASP page I need a function (or functions) that will do two things.

a) Firstly, strip a certain number of words from the end of a string. For example:

mystring = “the quick brown fox jumped over the lazy dog”

would display as

“the quick brown fox”

if I used something like this:

<%=mytrim(mystring, 5)%>

b) Secondly, strip a certain number of words from the beginning of a string. For example

myotherstring = “the quick brown fox jumped over the lazy dog”

would display as

“jumped over the lazy dog”

if I used something like this:

<%=myothertrim(myotherstring, 4)%>

Does this make sense? If so, is it possible and if so, how on earth is it done in classic ASP?

Any help would be fully appreciated

Best regards

Rod from the UK

PS: The closest I got was this (see below) but I couldn’t get it to work the other way:

<%    
Function TrimIt(sInput, iLength)  
    Dim aWords, iCounter, sOutput, sTmp  

    sOutput = sInput  

    If InStr(sInput, " ") > 0 Then  
        aWords = Split(sInput, " ")  
        For iCounter = 0 To UBound(aWords)  
            If Len(aWords(iCounter) & " ") + Len(sTmp) <= iLength Then  
                sTmp = sTmp & " " & aWords(iCounter)  
            Else  
                Exit For  
            End If  
        Next  
        sOutput = sTmp  
    End If  

    TrimIt = sOutput + "..."
End Function  
%>

<%=TrimIt(description, 150)%>

It’s your if statement which is killing you…

<%
 Function TrimEnd(strInput, intLength)
    arrWords = Split(strInput, " ")
    if UBound(arrWords) <= intLength then
          TrimEnd = strInput    
    else
          strTemp = ""
          For intCounter = 0 to intLength
                strTemp = strTemp & " " & arrWords(intCounter)
          Next
          TrimEnd = strTemp 
    end if
 End Function 

Function SkipWords(strInput, intLength)
    arrWords = Split(strInput, " ")
    if UBound(arrWords) <= intLength then
          SkipCount = ""    
    else
          strTemp = ""
          For intCounter = intLength to UBound(arrWords)
                strTemp = strTemp & " " & arrWords(intCounter)
          Next
          SkipWords = strTemp 
    end if
 End Function 

=TrimEnd(description, 10) 
=SkipWords(description, 10) 
%>

Hi Dave

This is absolutely spot on! Thank you so much!

The only thing I need to figure out now is how to flag if the description is under/over the 10 limit.

So, if it was less than 10 then I would display nothing on my page but if there were more than 10 then I would show a link saying something like “Read more”. Is this possible? If so how would I do it?

You’ve already helped me out so much so I fully understand if you feel you’ve done you bit. However, if you’re able to help me out again then that would be immensely appreciated!

Thanks again

Best regards

Rod Joseph

If the code falls into the if in each function, the string check is under the limit. If it falls into the else, there are more words than the limit, so handle accordingly

Of course!

Thanks again Dave

Best regards

Rod from the UK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.