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

StringshortenContent(String s, int maxLength)
shorten Content
if (s.length() > maxLength) {
    return s.substring(0, maxLength - 3) + "...";
} else {
    return s;
StringshortenCount(final long count)
Changes a large number into a binary postfixed version.
int unit = 1000;
if (count < unit) {
    return Long.toString(count);
int exponential = (int) (Math.log(count) / Math.log(unit));
char postfix = "KMBT".charAt(exponential - 1);
return String.format("%.1f%c", (count / Math.pow(unit, exponential)), postfix);
StringshortenCount(int count)
shorten Count
int shortCount = count;
String postFix = "";
if (count >= 1000000) {
    shortCount /= 1000000;
    postFix = "M";
} else if (count >= 1000) {
    shortCount /= 1000;
    postFix = "k";
...
StringshortenDbName(String dbName, int desiredLength)
Start by removing all vowels, then pull 1 letter at a time off the end of each _ separated segment, go until it is less than or equal to the desired length
StringBuilder dbBuf = new StringBuilder(dbName);
if (dbBuf.length() > desiredLength) {
    for (int i = dbBuf.length() - 1; i > 0; i--) {
        if (dbBuf.charAt(i - 1) == '_') {
            continue;
        char curChar = dbBuf.charAt(i);
        if (vowelBag.indexOf(curChar) > 0) {
...
StringshortenDerivedIdentifier(final String derivdedIdentifier)
shorten Derived Identifier
return shortenDerivedIdentifier(derivdedIdentifier, "_");
StringshortenFileName(String name)
shorten File Name
if (name == null)
    return name;
String retVal;
String tempStr = name.replace('\\', '/');
if (tempStr.indexOf('/') >= 0) {
    retVal = tempStr.substring(tempStr.lastIndexOf('/') + 1);
} else {
    retVal = name;
...
StringshortenFileName(String text, String filename)
shorten File Name
String fixed = filename.replace('\\', '/');
String shortname = filename;
if (fixed.lastIndexOf('/') >= 0)
    shortname = filename.substring(fixed.lastIndexOf('/') + 1, filename.length());
text = text.replaceAll(filename, shortname);
return text;
StringshortenGeneratedIdentifier(final String name)
shorten Generated Identifier
return shortenGeneratedIdentifier(name, "_");
StringshortenHash(String s)
shorten Hash
return s.substring(0, 8);
StringshortenLabel(String key)
Replace pre-determined keywords in a string, with shorter ones.
for (int i = 0; i < KEYWORDS.length; i++) {
    if (key.indexOf(KEYWORDS[i]) >= 0)
        key = key.replace(KEYWORDS[i], REPLACEMENTS[i]);
return key;