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

StringabbreviateLeft(String s, int width)
Abbreviates a String using ellipses inserted at left.
int length = s.length();
if (length > width) {
    if (width < 4) {
        throw new IllegalArgumentException("Minimal width is 4");
    return "..." + s.substring(length - (width - 3));
return s;
...
StringabbreviateMessage(String messageBody)
abbreviate Message
Integer start;
Integer end;
StringBuffer cutBody = new StringBuffer("");
while (messageBody.contains("<blockquote>") && messageBody.contains("</blockquote>")) {
    start = messageBody.indexOf("<blockquote>");
    end = messageBody.indexOf("</blockquote>");
    cutBody.append(messageBody.substring(0, start));
    cutBody.append(messageBody.substring(end + 13, messageBody.length()));
...
StringabbreviateMiddle(String str, String middle, int length)

Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String.

This abbreviation only occurs if the following criteria is met:

  • Neither the String for abbreviation nor the replacement String are null or empty
  • The length to truncate to is less than the length of the supplied String
  • The length to truncate to is greater than 0
  • The abbreviated String will have enough room for the length supplied replacement String and the first and last characters of the supplied String for abbreviation
Otherwise, the returned String will be the same as the supplied String for abbreviation.
if (isEmpty(str) || isEmpty(middle)) {
    return str;
if (length >= str.length() || length < (middle.length() + 2)) {
    return str;
int targetSting = length - middle.length();
int startOffset = targetSting / 2 + targetSting % 2;
...
StringabbreviateMiddle(String string, int maxLength)
abbreviate Middle
if (string == null || maxLength >= string.length()) {
    return string;
final int targetSting = maxLength - ELLIPSIS.length();
final int startOffset = targetSting / 2 + targetSting % 2;
final int endOffset = string.length() - targetSting / 2;
return string.substring(0, startOffset) + ELLIPSIS + string.substring(endOffset);
ObjectabbreviateObj(Object value, int maxWidth)
abbreviate Obj
return (value != null) ? abbreviate(value.toString(), maxWidth) : null;
StringabbreviateScript(String script)
Abbreviates this script to a length that will fit in the database.
if (script == null) {
    return null;
if (script.length() <= 1000) {
    return script;
return "..." + script.substring(3, 1000);
StringabbreviateText(final String text, final int numberOfCharacters, final String appendText)
Abbreviate a text to _approximately_ some number of characters, trying to find a nice word end and then appending some string (defaulting to three dots).
int nrChars = numberOfCharacters;
if (nrChars > 0 && text != null && text.length() > nrChars) {
    int spaceIndex = text.lastIndexOf(' ', nrChars);
    int dotIndex = text.lastIndexOf('.', nrChars);
    int commaIndex = text.lastIndexOf(',', nrChars);
    int questionIndex = text.lastIndexOf('?', nrChars);
    int exclamationIndex = text.lastIndexOf('!', nrChars);
    nrChars = spaceIndex;
...
StringabbreviateText(String text, int maxLength)
Abbreviates a long string using ellipsis.
return (text != null && text.length() > maxLength) ? text.substring(0, maxLength) + "..." : text;
Stringabbreviation(String description)
Given a string description, returns description if its length is no greater than 10, or description + "..."
return "\"" + description.substring(0, Math.min(10, description.length()))
        + (description.length() > 10 ? "(...)" : "") + "\"";
StringabbreviationtoLetter(String mutation)
abbreviationto Letter
return mutation.replaceAll("Ala", "A").replaceAll("Cys", "C").replaceAll("Glu", "E").replaceAll("Phe", "F")
        .replaceAll("Gly", "G").replaceAll("His", "H").replaceAll("Ile", "I").replaceAll("Lys", "K")
        .replaceAll("Leu", "L").replaceAll("Met", "M").replaceAll("Asn", "N").replaceAll("Hyp", "O")
        .replaceAll("Pro", "P").replaceAll("Gln", "Q").replaceAll("Arg", "R").replaceAll("Ser", "S")
        .replaceAll("Thr", "T").replaceAll("Glp", "U").replaceAll("Val", "V").replaceAll("Trp", "W")
        .replaceAll("Ter", "X").replaceAll("Tyr", "Y");