Is there a method in Java to automatically ellipsize a string? Just in Java, not other libraries.
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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):
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.