OK, I'm sure somebody, somewhere must have come up with an algorithm for this already, so I figured I'd ask before I go off to (re)invent it myself.

I have a list of arbitrary (user-entered) non-empty text strings. Each string can be any length (except 0), and they're all unique. I want to display them to the user, but I want to trim them to some fixed length that I decide, and replace part of them with an ellipsis (...). The catch is that I want all of the output strings to be unique.

For example, if I have the strings:

  • Microsoft Internet Explorer 6
  • Microsoft Internet Explorer 7
  • Microsoft Internet Explorer 8
  • Mozilla Firefox 3
  • Mozilla Firefox 4
  • Google Chrome 14

then I wouldn't want to trim the ends of the strings, because that's the unique part (don't want to display "Microsoft Internet ..." 3 times), but it's OK to cut out the middle part:

  • Microsoft...rer 6
  • Microsoft...rer 7
  • Microsoft...rer 8
  • Mozilla Firefox 3
  • Mozilla Firefox 4
  • Google Chrome 14

Other times, the middle part might be unique, and I'd want to trim the end:

  • Minutes of Company Meeting, 5/25/2010 -- Internal use only
  • Minutes of Company Meeting, 6/24/2010 -- Internal use only
  • Minutes of Company Meeting, 7/23/2010 -- Internal use only

could become:

  • Minutes of Company Meeting, 5/25/2010...
  • Minutes of Company Meeting, 6/24/2010...
  • Minutes of Company Meeting, 7/23/2010...

I guess it should probably never ellipsize the very beginning of the strings, even if that would otherwise be allowed, since that would look weird. And I guess it could ellipsize more than one place in the string, but within reason -- maybe 2 times would be OK, but 3 or more seems excessive. Or maybe the number of times isn't as important as the size of the chunks that rem

Edit
Report