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 value, int maxLen, String ellipses)
truncate
if (value.length() <= maxLen) {
    return value;
int end = maxLen;
for (int i = maxLen - 1; i >= 0; i--) {
    if (Character.isWhitespace(value.charAt(i))) {
        end = i + 1;
        break;
...
Stringtruncate(String x, int length)
String Truncator if a null or empty string is passed, an empty string is returned
if (x == null || x.length() == 0) {
    return "";
String ret = x;
if (x.length() > length) {
    ret = x.substring(0, length);
return ret;
...
StringtruncateAndTrailOffText(String text, int limit)
truncate And Trail Off Text
String trimmed = text.trim();
int dotsLength = F_DOTS.length();
int trimmedLength = trimmed.length();
int limitWithDots = limit - dotsLength;
if (limit >= trimmedLength) {
    return trimmed;
if (limit <= dotsLength) {
...
StringtruncateAndTrim0(String str, String delim)
truncate And Trim
if (str == null || delim == null) {
    return str;
int nStart = str.indexOf(delim);
if (nStart < 0) {
    return str;
return str.substring(0, nStart);
...
StringtruncateAt(final String input, final String... substrings)
Chops everything after (and including) substring.
int index = input.length();
for (String substring : substrings) {
    int value = input.indexOf(substring);
    if (value >= 0 && value < index) {
        index = value;
if (index == input.length()) {
...
StringtruncateAt(String str, char chr)
Trim the end off of a string starting at the specified character.
if (str == null) {
    return null;
int pos = str.indexOf(chr);
if (pos < 0) {
    return str;
return str.substring(0, pos);
...
StringtruncateAtMaxLength(String source, int maxLength, boolean addEllipsis)
If this given string is of length maxLength or less, it will be returned as-is.
if (source.length() <= maxLength) {
    return source;
if (addEllipsis && maxLength > 3) {
    return unicodePreservingSubstring(source, 0, maxLength - 3) + "...";
return unicodePreservingSubstring(source, 0, maxLength);
StringtruncateAtWhitespace(String text, int length)
Truncate text on a whitespace boundary (near a specified length).
int desired, lowerBound, upperBound;
if (length < MINIMUM_SUPPORTED_LENGTH) {
    throw new IllegalArgumentException(
            "Requested length too short (must be " + MINIMUM_SUPPORTED_LENGTH + " or greated)");
if (text.length() <= length) {
    return text;
lowerBound = length / 4;
upperBound = length + (length / 2);
for (int i = length - 1; i > lowerBound; i--) {
    if (Character.isWhitespace(text.charAt(i))) {
        return text.substring(0, i);
for (int i = (length); i < upperBound; i++) {
    if (Character.isWhitespace(text.charAt(i))) {
        return text.substring(0, i);
return text.substring(0, length);
StringtruncateAtWord(String input, int length)
Returns first part of string upto specified length (breaks at word, greedy)
int offset;
offset = 2;
if (input == null || input.length() < (length - offset)) {
    return input;
int iNextSpace;
iNextSpace = input.lastIndexOf(" ", length);
String trunc = input;
...
StringtruncateBodyText(String input)
Truncates kb article content.
int limit = 300;
if (input.length() > limit) {
    input = input.substring(0, limit) + "...";
return input;