Truncate strings with ellipsis

Truncate strings with ellipsis

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.


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",

    Requires Free Membership to View

    Register today to access targeted resources from our editorial writers and independent industry experts focused on Lotus Domino, Notes, Workplace and other related technologies.

    By submitting your registration information to SearchDomino.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchDomino.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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

This was first published in July 2002

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.