Making a long name shorter within a range

This is just a small JAVA snippet to reduce a given long name onto a format that fits a max size.


* Provides a text that will match a desired dimension, reducing
* it if necessary.

public static String shortText(String text, int maxLength){
String result = text;

// if this text portion is bigger than allowed, reduce
if(text.length() > maxLength){
int half = maxLength / 2;
int length = text.length();
result = text.substring(0, half) + ".."
+ text.substring(length - half, length);

return result;

No comments:

Post a Comment