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

Stringshorten(String s, int length, String suffix)
shorten
if (s == null || suffix == null) {
    return null;
if (s.length() > length) {
    s = s.substring(0, length) + suffix;
return s;
Stringshorten(String s, int length, String suffix)
shorten
StringBuilder sb = null;
if (s != null && suffix != null && s.length() > length) {
    for (int j = length; j >= 0; j--) {
        if (Character.isWhitespace(s.charAt(j))) {
            length = j;
            break;
    sb = new StringBuilder();
    sb.append(s.substring(0, length));
    sb.append(suffix);
return (sb == null ? null : sb.toString());
Stringshorten(String s, int max)
Shortens a string (and appends "...") if longer than the allowed maximum number of characters.
if (s.length() > max)
    return s.substring(0, max) + "...";
else
    return s;
Stringshorten(String s, int maxLength)
Returns a single line string that is no longer than maxLength .
if (maxLength < 5)
    maxLength = 5;
boolean isTruncated = false;
int brIdx = s.indexOf('\n');
if (brIdx != -1) {
    s = s.substring(0, brIdx);
    isTruncated = true;
;
brIdx = s.indexOf('\r');
if (brIdx != -1) {
    s = s.substring(0, brIdx);
    isTruncated = true;
if (s.length() > maxLength) {
    s = s.substring(0, maxLength - 3);
    isTruncated = true;
if (!isTruncated) {
    return s;
} else {
    if (s.endsWith(".")) {
        if (s.endsWith("..")) {
            if (s.endsWith("...")) {
                return s;
            } else {
                return s + ".";
        } else {
            return s + "..";
    } else {
        return s + "...";
Stringshorten(String script, int length)
shorten
String shortened = script.length() > length ? script.substring(0, length).intern() + "..." : script;
return shortened.replace("\n", " ").replace("\r", " ").replace("\t", " ");
Stringshorten(String str, int length)
shorten
if (length < 3) {
    throw new IllegalArgumentException("Invalid length");
String s;
if (str.length() <= length) {
    s = str;
} else {
    s = str.substring(0, length - 3) + "...";
...
Stringshorten(String str, int length)
shorten
if (str.length() <= length)
    return str;
return str.substring(0, length - 3) + "...";
Stringshorten(String string, boolean isPrefix)
shorten
if (string.length() > 10)
    if (isPrefix)
        return "..." + string.substring(string.length() - 10);
    else
        return string.substring(0, 10) + "...";
else
    return string;
Stringshorten(String string, int size)
shorten
if (string == null) {
    return null;
if (size < string.length()) {
    return string.substring(0, size - 1) + "...";
} else {
    return string;
Stringshorten(String string, int upTo)
Shorten the given string if the string is more than the given length.
if (string.length() > upTo) {
    StringBuilder builder = new StringBuilder(upTo + 10);
    return builder.append(string.substring(0, upTo)).append("... ").append(string.length() - upTo)
            .append(" more").toString();
return string;