title: Ideal method to truncate a string with ellipsis
body: <p>I'm sure all of us have seen ellipsis' on Facebook statuses (or elsewhere), and clicked "Show more" and there are only another 2 characters or so. I'd guess this is because of lazy programming, because surely there is an ideal method.</p>
  
  <p>Mine counts slim characters <code>[iIl1]</code> as "half characters", but this doesn't get around  ellipsis' looking silly when they hide barely any characters.</p>
  
  <p>Is there an ideal method? Here is mine:</p>
  
  <pre><code>/**
   * Return a string with a maximum length of &lt;code&gt;length&lt;/code&gt; characters.
   * If there are more than &lt;code&gt;length&lt;/code&gt; characters, then string ends with an ellipsis ("...").
   *
   * @param text
   * @param length
   * @return
   */
  public static String ellipsis(final String text, int length)
  {
      // The letters [iIl1] are slim enough to only count as half a character.
      length += Math.ceil(text.replaceAll("[^iIl]", "").length() / 2.0d);
  
      if (text.length() &gt; length)
      {
          return text.substring(0, length - 3) + "...";
      }
  
      return text;
  }
  </code></pre>
  
  <p>Language doesn't really matter, but tagged as Java because that's what I'm mostly interested in seeing.</p>