ASP: Trim Text Function
Introduction
Author: Martin WarningYear: 2007
Updated: 2008
License: Free
These are simple functions that will take a string of text as input and trim it to the ammount of characters desired. Instead of cutting the text at that length it will cut the text after the word has finished. This means that the function will show X ammount of character plus the ammount of character it takes to finish the word that is being cut. This is a function that can be useful for blogs, news sites or calendars where you want to show a list of items with a short description. You can use this function to create this short description based on the complete text of the item.
Triming text is very straightforward in ASP, you can use the ASP Left function like Left(strSomeText,20) which would cut the strSomeText variable to a length of 20 characters. These functions go beyond that by not cutting at 20 characters which could be in the middle of a word, but instead these functions will look for the end of the word and than cut off the text.
Code
Languages:- ASP
- VBScript
<% Function fncTrimText(strText,intCharacters,blnMore) ' The function uses the following variables ' strText is used to pass the text to be trimmed ' intCharacters is used to indicate at what ammount of characters the text should be trimmed ' blnMore 0 for no and 1 for yes is used to add 3 dots after the trimmed text if the original text is longer ' Declare the variables required Dim blnDone : blnDone = 0 Dim strTextTrimmed ' First trim the text to the desired ammount of characters strTextTrimmed = Left(strText,intCharacters) ' Then add the remaining characters of the word if a word was cut with the trimming If cInt(Len(strText)) > cInt(intCharacters) Then intCharacters = intCharacters + 1 Do While (blnDone < 1) If Mid(strText,intCharacters,1) = " " Then blnDone = 1 Else strTextTrimmed = strTextTrimmed & Mid(strText,intCharacters,1) End If If cInt(Len(strText)) <= cInt(intCharacters) Then blnDone = 1 End If intCharacters = intCharacters + 1 Loop ' Now add the ... after the trimmed text If blnMore = 1 Then strTextTrimmed = strTextTrimmed & " ... " End If End If fncTrimText = strTextTrimmed End Function %>
If so desired the function could be modified to cut at the end of a sentence. The line If Mid(strText,intCharacters,1) = " " Then would need to be changed to If Mid(strText,intCharacters,1) = "." Then to accomplish that taking in account that the dot is only used to indicate the end of a sentence. To change the 3 dots text to a different text you need to change the ... in the strTextTrimmed = strTextTrimmed & " ... " line to whatever you want to show.
The following code is a different implementation:
<% Function fncTrimText(strText,intCharacters) ' The function uses the following variables ' strText is used to pass the text to be trimmed ' intCharacters is used to indicate at what ammount of characters the text should be trimmed If Len(strText) > intCharacters Then If inStr(100,strText," ") > intCharacters Then strText = Left(strText,inStr(intCharacters,strText," ")) & "..." ElseIf inStr(intCharacters,strText," ") = intCharacters Then strText = Left(strText,intCharacters) & "..." End If End If fncTrimText = strText End Function %>
This function is less code than the first example. Just like the first example you could instead of cutting on words cut on sentences and the . instead of a space in inStr functions.