Java Utililty Methods String Ellipse

List of utility methods to do String Ellipse

Description

The list of methods to do String Ellipse are organized into topic(s).

Method

StringellipsisString(String text, int ellipsisAt)
ellipsis String
if (text.length() > ellipsisAt) {
    return text.substring(0, ellipsisAt) + "...";
} else {
    return text;
StringellipsisText(String text, int maxLength)
ellipsis Text
return text.length() <= maxLength ? text : text.substring(0, maxLength - 3) + "...";
Stringellipsize(String input, int maxLength)
ellipsize
String ellip = "...";
if (input == null || input.length() <= maxLength || input.length() < ellip.length()) {
    return input;
return input.substring(0, maxLength - ellip.length()).concat(ellip);
Stringellipsize(String s, int maxChars)
ellipsize
if (s == null)
    return null;
if (maxChars < 4)
    return (s.substring(0, maxChars));
if (s.length() > maxChars)
    return s.substring(0, maxChars - 3) + "...";
else
    return s;
...
Stringellipsize(String string, int length)
Ellipsize a string, keeping it readable
String ellipsis = "...";
if (length >= string.length()) {
    return string;
int trim = length - ellipsis.length();
return string.substring(0, trim / 2) + ellipsis + string.substring(string.length() - trim / 2);
Stringellipsize(String text, int max)
ellipsize
return cut(text, max, "...", false);
Stringellipsize(String text, int max)
From StackOverflow
if (textWidth(text) <= max)
    return text;
int end = text.lastIndexOf(' ', max - 3);
if (end == -1)
    return text.substring(0, max - 3) + "...";
int newEnd = end;
do {
    end = newEnd;
...
Stringellipsize(String text, int maxLength)
Cuts the string at the end if it's longer than maxLength and appends "..."
if (text != null && text.length() > maxLength) {
    return text.substring(0, maxLength - 3) + "...";
return text;
StringellipsizeKeepingExtension(String s, int maxChars)
converts a very long name.doc -> a very lon...g.doc.
if (s.length() <= maxChars)
    return s;
int idx = s.lastIndexOf(".");
if (idx <= 0)
    return ellipsize(s, maxChars); 
int MAX_EXTENSION_LENGTH = 6;
if (s.length() - idx > MAX_EXTENSION_LENGTH)
    return ellipsize(s, maxChars); 
...