Java Utililty Methods String Abbreviate

List of utility methods to do String Abbreviate

Description

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

Method

Stringabbreviate(String str, int offset, int maxWidth)
abbreviate
if (str == null) {
    return null;
if (maxWidth < 4) {
    throw new IllegalArgumentException("Minimum abbreviation width is 4");
if (str.length() <= maxWidth) {
    return str;
...
Stringabbreviate(String str, int preferredLength)
Abbreviates a string for display if necessary.
return abbreviate(str, preferredLength, 0, true);
Stringabbreviate(String string, int maxLength)
Shortens a String if it is longer than the given maxLength, ending an abbreviated String with "...".
return string.length() <= maxLength ? string : string.substring(0, maxLength - 3) + "...";
Stringabbreviate(String text, int maxFront, int maxBack)
abbreviate
if (text.length() > maxFront + maxBack - 3) {
    text = text.substring(0, maxFront) + "..." + text.substring(text.length() - maxBack, text.length());
return text;
Stringabbreviate(String text, Number maxNbrChars)
abbreviate
return abbreviate(text, maxNbrChars, "...");
Stringabbreviate(String time)
abbreviate
time = time.replaceAll(" days", "d");
time = time.replaceAll(" day", "d");
time = time.replaceAll(" hours", "hr");
time = time.replaceAll(" hour", "hr");
time = time.replaceAll(" minutes", "m");
time = time.replaceAll(" minute", "m");
time = time.replaceAll(" seconds", "s");
time = time.replaceAll(" second", "s");
...
Stringabbreviated(final String str, final int maxLength)
abbreviated
return str != null ? (str.length() < maxLength ? str : str.substring(0, maxLength - 3) + "...") : null;
StringabbreviateDotSeparatedString(String string, int targetLength)
Abbreviates the given dot-separated string to reduce its length (if necessary) to within the supplied target length.
if (string.length() <= targetLength) {
    return string;
String[] components = string.split(DOT_SEPARATOR_SPLIT_REGEX);
int actualLength = string.length();
StringBuilder builder = new StringBuilder();
int index = 0;
while (actualLength > targetLength && index < components.length - 1) {
...
StringabbreviateFileName(final String fileName)
Create a truncated file name suitable for display in interfaces
if (fileName == null) {
    throw new IllegalArgumentException("null fileName");
StringBuilder sb = new StringBuilder();
if (fileName.length() < 100) {
    sb.append(fileName);
} else {
    sb.append(fileName.substring(0, 50));
...
StringabbreviateInCenter(String stringToAbbreviate, int length)
abbreviate In Center
int halfLength = length / 2;
int firstPartLastIndex = halfLength - ELLIPSIS.length();
int stringLength = stringToAbbreviate.length();
return String.format("%s%s%s", stringToAbbreviate.substring(0, firstPartLastIndex), ELLIPSIS,
        stringToAbbreviate.substring(stringLength - halfLength, stringLength));