Is there a method in Java to automatically ellipsize a string? Just in Java, not other libraries.
Thanks.
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 charactersAfterEllipsis the number of characters that should appear after the ellipsis (0 or larger)
* @return the truncated string with trailing ellipses
*/
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.
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 :-)
This method will return the ellipsized String:
String ellipsize(String input, int maxLength) {
String ellip = "...";
if (input == null || input.length() <= maxLength
|| input.length() < ellip.length()) {
return input;
}
return input.substring(0, maxLength - ellip.length()).concat(ellip);
}
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) + "...";
}
A oneliner which of course does not outperform the other offered solutions:
longString.replaceFirst("(.{5}).+(.{5})", "$1...$2")
Will leave only first and last 5 characters and put ... in the middle.