StrCutOff Function:
This function allows to control the length of a string.
Description:
This function allows you to control the length of a string. The function allows to add text like "..." to the end of an adjusted string to indicate that it has been truncated. Many applications make a truncation for an article to fit its section. We can always take the first several characters from the article to make its abstract as well. But this method sometimes will break a whole word. This function is ready to handle this situation and avoid the broken word problem.
Syntax:
Response.Write StrCutOff(text, MaxLength, strAdd)
Details:
There are three inputs: text: the string that should be shortened MaxLength: the max number of character’s before truncation strAdd: string added after truncation
The example below returns: Lorem ipsum dolor sit amet, consectetuer ....more
Example:
<%
Dim text text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis pharetra interdum sem." Response.Write StrCutOff(text, 50, " ....more")
%>
ASP Source Code:
<%
Private Function StrCutOff(text, MaxLength, strAdd) Dim atext, i, sOutput, sTemp, sDot text = Trim(text) if Len(text) > MaxLength Then If strAdd = "" Then sDot = "...." Else sDot = strAdd End if atext = Split(text, " ") For i = 0 To Ubound(atext) if Len(sOutput) >= MaxLength Then StrCutOff = sOutput & sDot Exit For Else if i = 0 Then sTemp = atext(i) Else sTemp = sOutput & " " & atext(i) End if if Len(sTemp) >= MaxLength Then StrCutOff = sOutput & sDot Exit For Else sOutput = sTemp End if End if Next Else StrCutOff = text End if End Function
%>
|