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

StringtruncateWithDefault(String value, int size, String defaultValue)
truncateWithDefault.
if (isEmpty(value)) {
    return defaultValue;
if (value.length() > size) {
    return defaultValue;
return value;
StringtruncateWithEllipsis(String str, int maxLength)
truncate With Ellipsis
StringBuilder sb = new StringBuilder();
if (str != null) {
    sb.append(str);
    if (str.length() > maxLength) {
        sb.setLength(maxLength);
        if (maxLength > 3) {
            sb.replace(maxLength - 3, maxLength, "...");
return sb.toString();
StringtruncAtWord(String a_text, int a_length)
Truncate a string at a word boundary
String text = a_text;
if (null != a_text && text.length() > a_length) {
    int backEnd = 20;
    if (backEnd > a_length) {
        backEnd = a_length - 1;
    int finalLength = a_length;
    char character;
...
StringtruncString(String in, int len)
Simple static method to tuncate Strings to given length.
String out = in;
if (in != null && len > 0 && in.length() > len) {
    out = in.substring(0, len);
return out;
Stringtruncuate(String s, int maximumLength, String append)
truncuate
if (s.length() > maximumLength) {
    return s.substring(0, maximumLength - append.length()) + append;
return s;
StringtruncVal(String s)
trunc Val
if (s == null) {
    return "";
int len = Math.min(s.length(), 10);
String postfix = "";
if (s.length() > len) {
    postfix = "...";
return s.substring(0, len) + postfix;