Note that there are some explanatory texts on larger screens.

plurals
  1. POEllipsizing a set of names
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>For example, if I have the strings:</p> <ul> <li>Microsoft Internet Explorer 6</li> <li>Microsoft Internet Explorer 7</li> <li>Microsoft Internet Explorer 8</li> <li>Mozilla Firefox 3</li> <li>Mozilla Firefox 4</li> <li>Google Chrome 14</li> </ul> <p>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:</p> <ul> <li>Microsoft...rer 6</li> <li>Microsoft...rer 7</li> <li>Microsoft...rer 8</li> <li>Mozilla Firefox 3</li> <li>Mozilla Firefox 4</li> <li>Google Chrome 14</li> </ul> <p>Other times, the middle part might be unique, and I'd want to trim the end:</p> <ul> <li>Minutes of Company Meeting, 5/25/2010 -- Internal use only</li> <li>Minutes of Company Meeting, 6/24/2010 -- Internal use only</li> <li>Minutes of Company Meeting, 7/23/2010 -- Internal use only</li> </ul> <p>could become:</p> <ul> <li>Minutes of Company Meeting, 5/25/2010...</li> <li>Minutes of Company Meeting, 6/24/2010...</li> <li>Minutes of Company Meeting, 7/23/2010...</li> </ul> <p>I guess it should probably never ellipsize the <em>very</em> 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 remain: less than about 5 characters between ellipses would be rather pointless.</p> <p>The inputs (both number and size) won't be terribly large, so performance is not a major concern (well, as long as the algorithm doesn't try something silly like enumerating all possible strings until it finds a set that works!).</p> <p>I guess these requirements seem pretty specific, but I'm actually fairly lenient -- I'm just trying to describe what I have in mind.</p> <p>Has something like this been done before? Is there some existing algorithm or library that does this? I've googled some but found nothing quite like this so far (but maybe I'm just bad at googling). I have to believe somebody somewhere has wanted to solve this problem already!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload