March 16th, 2006

ASP: Trim a String Without Cutting Any Words

This function will accept a string of any length, and trim it to the space nearest your desired trimming length.

'====
Function neatTrim( strToTrim, desiredLength )
'====
    strToTrim = trim( strToTrim )
   
    if len( strToTrim ) < desiredLength then
        neatTrim = strToTrim
        exit function
    else
        if inStrRev( strToTrim, " ", desiredLength ) = 0 then
            strToTrim = left( strToTrim, desiredLength - 1 ) & "&#133;"
         else
            strToTrim = left( strToTrim, inStrRev( strToTrim, " ", desiredLength + 1 ) -1 ) & "&#133;" 'no carriage return here
        end if
    end if
   
    neatTrim = trim( strToTrim )
End Function

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image