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(final String s, final int n)
Retrieves the first n characters of s, or all of s if its length is less than n
return length(s) <= n ? s : s.substring(0, n);
Stringtruncate(final String str, final int len)
truncate
return (str.length() <= len) ? str : str.substring(0, len);
Stringtruncate(final String str, final int len)
truncate a string if it longer than the argument
if (str == null) {
    throw new NullPointerException("String is null");
if (len < 0) {
    throw new IndexOutOfBoundsException("Length is less than zero");
if (str.length() > len) {
    return str.substring(0, len);
...
Stringtruncate(final String str, final int maxWidth)
Method to truncate the string when max width is given
return truncate(str, 0, maxWidth);
Stringtruncate(final String target, final int maxSize)
truncate
return (target.length() > maxSize ? target.substring(0, maxSize) : target);
Stringtruncate(final String value, final int width)
Truncate a string if the desired with is less than the length of the input string.
if (width >= value.length()) {
    return value;
return value.substring(0, width - 3) + "...";
Stringtruncate(Object truncateMe, int maxLength, String suffix)
Limits the string value of 'truncateMe' to the specified max length in characters.
return truncate(truncateMe, maxLength, suffix, true);
Stringtruncate(String eval, String suffix, int targetLength)
truncate
if (eval.length() > targetLength)
    return eval.substring(0, targetLength - 1) + suffix;
else
    return eval;
Stringtruncate(String input, int maxLength)
Truncates the passed String to the passed maximum length, if it exceeds the maximum length.
if (input != null && input.length() > maxLength) {
    return input.substring(0, maxLength);
return input;
Stringtruncate(String input, int maxLength)
Truncates the input sting so that it is not longer than maxLength.
String ellipsis = "\u2026";
return truncate(input, maxLength, ellipsis);