tags:

views:

145

answers:

4

Is there a method in Java to automatically ellipsize a string? Just in Java, not other libraries.

Thanks.

+3  A: 

There is not. But here's another crack at a simple method to do this.

String ellipsize(String input, int maxLength) {
  if (input == null || input.length() < maxLength) {
    return input;
  }
  return input.substring(0, maxLength) + "...";
}
Sean Owen
A: 

Sure, try this one:

public static String ellipsise (String input, int maxLen) {
    if (input == null)
        return null;
    if ((input.length() < maxLen) || (maxLen < 3))
        return input;
    return input.substring (0, maxLen - 3) + "...";
}

This has the advantage of fixing the bug where the King's English is not used properly :-)

paxdiablo
+3  A: 

This method will return the ellipsized String:

String ellipsize(String input, int maxLength) {
    String ellip = "...";
    if (input == null || input.lenght() <= maxLength 
           || input.length() < ellip.length()) {
        return input;
    }
    return input.substring(0, maxLength - ellip.length()).concat(ellip);
}
cuh
+2  A: 

Depending on your use case, it might be useful to put the ellipsis between letters (i.e. add letters at the end to provide some context):

/**
 * Puts ellipses in input strings that are longer than than maxCharacters. Shorter strings or
 * null is returned unchanged.
 * @param input the input string that may be subjected to shortening
 * @param maxCharacters the maximum characters that are acceptable for the unshortended string. Must be at least 3, otherwise a string with ellipses is too long already.
 * @param the number of characters that should appear after the ellipsis (0 or larger) 
 */
public static String ellipsize(String input, int maxCharacters, int charactersAfterEllipsis) {
  if(maxCharacters < 3) {
    throw new IllegalArgumentException("maxCharacters must be at least 3 because the ellipsis already take up 3 characters")
  }
  if(maxCharacters - 3 > charactersAfterEllipsis) {
    throw new IllegalArgumentException("charactersAfterEllipsis must be less than maxCharacters");
  }
  if (input == null || input.length() < maxCharacters) {
    return input;
  }
  return input.substring(0, maxCharacters - 3 - charactersAfterEllipsis) + "..." + input.substring(input.length() - charactersAfterEllipsis);
}

There are also more sophisticated features you might want from you ellipsis algorithm: If you need to place the text into a graphical element and you are using a proportional font, then you must measure the length of the String.

For Swing/AWT that would be java.awt.Font.getStringBounds. In such a case, a simple algrithm would cut the string one letter at a time and add the ellipsis, until the string fits into the limit given. If used often, the bisection method elaborated in http://www.codeproject.com/KB/cs/AutoEllipsis.aspx?msg=3278640 (C#, but should be easy enough to translate to Java) may save some processor cycles.

nd
I have implemented something along the lines of what you suggested. Thanks!I think I'll try to provide a general String-ellipsising class, for now I needed a quick hack inside a class, but it's worth trying to invest some time and develop a good API.
Milo Casagrande