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 string, int length)
Truncates string at given length, appends "..." if string was shortened
return string.length() > length ? string.substring(0, length) + "..." : string;
Stringtruncate(String string, int maxLength, boolean atBeginning, String ellipse)
Returns a truncated version of the string, where the removed part is marked by ellipse.
int length = string.length();
int removedLength = length - maxLength - ellipse.length();
if (removedLength > 0) {
    string = (atBeginning ? ellipse + string.substring(removedLength)
            : string.substring(0, length - removedLength) + ellipse);
return string;
Stringtruncate(String string, int maxSize)
This method truncates the given String value to the given size, but appends an ellipse ("...") if the String is larger than maxSize.
string = toNull(string);
if (string.length() <= maxSize)
    return string;
return String.format("%s...", string.subSequence(0, maxSize - 3));
Stringtruncate(String string, int n, String suffix)
truncate
if (string.length() - suffix.length() > n) {
    return string.substring(0, n) + suffix;
return string;
Stringtruncate(String strOrginal, int iByteMaxSize)
truncate
int iByteLen = 0;
final int strLen = strOrginal.length();
if (iByteMaxSize < 4) {
    return "...".substring(0, iByteMaxSize);
for (int i = 0; i < strLen; i++) {
    iByteLen += ((strOrginal.charAt(i) & 0xFF00) == 0) ? 1 : 2;
    if (iByteLen > iByteMaxSize) {
...
Stringtruncate(String text)
Truncates a string from the end (right) in one character
return truncate(text, 1, false);
Stringtruncate(String text, int len)
Truncate the supplied text to a maximum of len codepoints.
if (text.codePointCount(0, text.length()) <= len) {
    return text;
return firstN(text, len - 1) + TRUNCATE_CHAR;
Stringtruncate(String text, int length)
Truncates the string to a particular length if it's longer and appends ...
if (text == null || text.length() <= length) {
    return text;
return new String(ellipsify(text.substring(0, length)));
Stringtruncate(String text, int length)
truncate
return text.length() > length ? text.substring(0, length - 1) + "..." : text;
Stringtruncate(String text, int maxLength)
truncate
if (text == null || text.length() <= maxLength) {
    return text;
} else if (maxLength <= 3) {
    return TRUNCATED_TEXT_SUFFIX;
} else {
    return text.substring(0, maxLength - 3) + TRUNCATED_TEXT_SUFFIX;