Java Utililty Methods String Shorten

List of utility methods to do String Shorten

Description

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

Method

StringshortenMiddle(String msg, int maxLen)
Inserts "..."
if (msg.length() <= maxLen)
    return msg;
int half = (maxLen - 3) / 2;
return msg.substring(0, half) + "..." + msg.substring(msg.length() - half);
StringshortenName(final String fullName)
shorten Name
String result;
if (fullName.isEmpty()) {
    result = "";
} else {
    result = removePrefix(fullName);
    result = cleanSuffix(result);
return result;
...
StringshortenName(final String name, final int toBeAdded)
shorten Name
if (name == null)
    return "";
final int symbols = Math.max(10, 20 - toBeAdded);
if (name.length() < symbols)
    return name;
else
    return name.substring(0, symbols) + "...";
StringshortenName(String name)
shorten Name
int i = name.lastIndexOf('.');
if (i >= 0 && i <= name.length() - 1) {
    name = name.substring(i + 1);
return name;
StringshortenOperationName(String operation)
Shortens full qualified method names.
String mainPart = operation.substring(0, operation.indexOf("("));
String[] packages = mainPart.split("\\.");
String result = "";
for (int i = 0; i < packages.length - 2; i++) {
    result += packages[i].substring(0, 1);
    result += ".";
result += packages[packages.length - 2] + "." + packages[packages.length - 1];
...
StringshortenPackageName(String name, boolean remove)
Shortens package part of the name of a class.
final String[] comp = name.split("\\.");
final int last = comp.length - 1;
if (remove) {
    return comp[last];
final StringBuilder s = new StringBuilder();
for (int i = 0; i < last; i++) {
    s.append(comp[i].substring(0, 1)).append(".");
...
charshortenPart(String currentPart)
shorten Part
return currentPart.charAt(0);
StringshortenPath(final String path)
Shortens the path to a maximum of 4 path elements.
return shortenPath(path, DEFAULT_SHORTENER_THRESHOLD);
StringshortenPrefix(String prefix)
shorten Prefix
String result = null;
int idx = prefix.lastIndexOf('/');
if (idx > 0) {
    result = prefix.substring(0, idx);
return result;
StringshortenRemoteRef(final String origin, final String ref)
Cut the origin part off a remote ref.
if (!ref.startsWith(origin + "/")) {
    return ref;
return ref.substring(origin.length() + 1);