When copying data to a place where you can only store or display a limited number of characters, this little function will trim the string to a specified length, adding "..." to show that some characters were removed.
Note: in the LotusScript version of this function, "Byval" is not used for the string parameter because we expect this function might be used with longer strings, where the overhead of copying the string might exceed the overhead of referring to the string twice by reference. I haven't actually done timing tests, because the performance difference is likely trivial.
Code
Javascript version:
function ellipsis(x, maxlen)
{
/* given a string and a maximum length for the string, this routine
returns the same string truncated to the maximum length. In addition,
if the string was truncated, "..." is added to the end, again not to
exceed the maximum length.
E.g. ellipsis("abcdef", 4) = "a..."
ellipsis("abcdef", 6) = "abcdef"
*/
if (x.length <= maxlen)
return x
else if (maxlen < 4)
return x.substring(0, maxlen) // no room for ellipsis
else return x.substring(0, maxlen-3) + "...";
}
LotusScript version:
Function ellipsis(x As String, Byval maxlen%) As String
' Given a string and a maximum length for the string, this routine
' returns the same string truncated to the maximum length. In addition,
' if the string was truncated, "..." is added to the end, again not to
' exceed the maximum length.
' E.g. ellipsis("abcdef", 4) = "a..."
' ellipsis("abcdef", 6) = "abcdef"
If Len(x) <= maxlen Then
ellipsis = x
Elseif maxlen < 4 Then
ellipsis = Left$(x, maxlen)
Else
ellipsis = Left$(x, maxlen-3) & "..."
End If
End Function