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 source, int maxLength, String cutoffReplacement)
Makes a best efford to truncate a string into the desired length.
if (source == null || source.length() <= maxLength)
    return source;
int cutoffLength = maxLength;
if (cutoffReplacement != null)
    cutoffLength -= cutoffReplacement.length();
if (cutoffLength <= 0) {
    if (cutoffReplacement != null)
        return cutoffReplacement;
...
Stringtruncate(String src, int maxChars, boolean addEllipses)
truncate
if (src != null) {
    if (src.length() > maxChars) {
        StringBuilder result = new StringBuilder(src.substring(0, maxChars));
        if (addEllipses && maxChars < src.length()) {
            result.append("...");
        return result.toString();
return src;
Stringtruncate(String str)
Shortens the given string if its length exceeds a fixed limit.
if (str.length() > 32)
    return str.substring(0, 32) + "..."; 
return str;
Stringtruncate(String str, int byteLength)
truncate
if (str == null) {
    return null;
if (str.length() == 0) {
    return str;
if (byteLength < 0) {
    throw new IllegalArgumentException("Parameter byteLength must be great than 0");
...
Stringtruncate(String str, int chars)
truncate
if (str == null)
    return str;
if (str.length() > chars) {
    return str.substring(0, chars - 3) + "...";
return str;
Stringtruncate(String str, int len)
truncate
if (str == null)
    return "";
String crop = str;
int slen = 0;
int blen = 0;
char c;
try {
    while (blen + 1 <= len) {
...
Stringtruncate(String str, int length)
Truncates a string based on the specified length.
if (str == null) {
    return null;
if (str.length() > length) {
    return str.substring(0, length);
return str;
Stringtruncate(String str, int length)
Truncate a String to a specified length
if (str == null || length < 0)
    return str;
else
    return str.length() > length + 1 ? str.substring(0, length) : str;
Stringtruncate(String str, int length)
truncate
if (length > str.length())
    return str;
char c;
int index = length;
while ((Character.isWhitespace(c = str.charAt(index)) == false) && (index > 0)) {
    index--;
if (index == 0)
...
Stringtruncate(String str, int length)
Truncates the given string so it is no longer than the given length, which must be at least three.
assert length >= 3 : length;
length = length - 3;
if (str.length() <= length)
    return str;
else
    return str.substring(0, length) + "...";