Java Utililty Methods String Truncate

List of utility methods to do String Truncate

Description

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

Method

Stringtruncate(String s, int i)
Truncates a String s to length i or less
if (s == null) {
    return null;
if (i < 1) {
    return null;
if (s.length() > i) {
    return s.substring(0, i);
...
Stringtruncate(String s, int length)
Truncate the input string to be at most the input length
if (s.length() <= length)
    return s;
return s.substring(0, length);
Stringtruncate(String s, int length)
Truncates a string to a maximum length.
length -= 3;
if (s.length() > length)
    s = s.substring(0, length) + "...";
return s;
Stringtruncate(String s, int length, boolean indicator)
truncate
if (isBlank(s) || s.length() <= length)
    return s;
if (indicator)
    return s.substring(0, length - 3) + "...";
else
    return s.substring(0, length);
Stringtruncate(String s, int max_len)
truncate
if (s == null)
    s = "???";
int len = s.length();
if (len <= max_len) {
    for (int i = 0; i < max_len - len; i++)
        s = s + " ";
} else
    s = s.substring(0, max_len - 3) + "...";
...
Stringtruncate(String s, int maxLength)
truncate
if (s.length() > maxLength) {
    StringBuilder sb = new StringBuilder();
    sb.append(s.substring(0, maxLength - 3));
    sb.append("...");
    return sb.toString();
return s;
Stringtruncate(String s, int maxLength)
Truncate the specified String if it is longer than maxLength.
return truncate(s, maxLength, "");
Stringtruncate(String s, int maxLength, boolean alignRight)
truncate
if (s.length() <= maxLength)
    return s;
return alignRight ? s.substring(s.length() - maxLength, s.length()) : s.substring(0, maxLength);
Stringtruncate(String s, int stop, String suf)
truncate
if (s == null) {
    return "";
if (s.length() == 0) {
    return "";
} else if (s.length() <= stop) {
    return s;
} else {
...
Stringtruncate(String s, int truncLength, boolean removeWhiteSpace)
truncate
String temp = ((s.length() > truncLength) ? (s.substring(0, truncLength) + "...") : s);
if (removeWhiteSpace) {
    temp = temp.replaceAll("\\s+", " ");
return temp;