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

StringtruncateMessage(String message, int TRUNCATED_MSG_SIZE)
truncate Message
if (message != null && message.length() > TRUNCATED_MSG_SIZE)
    return message.substring(0, TRUNCATED_MSG_SIZE) + "...";
else
    return message;
StringtruncateMessageIn50000Characters(final String message)
truncate Message In Characters
return truncateMessageInCharacters(message, 50000);
StringtruncateName(String name)
truncate Name
if (name == null || name.length() <= 12)
    return name;
else
    return name.substring(0, 12);
StringtruncateName(String name)
Returns name truncated to DISPLAY_WIDTH.
if (name.length() > DISPLAY_WIDTH) {
    return name.substring(0, DISPLAY_WIDTH);
} else {
    return name;
StringtruncateNicely(String s, int n, String suffix)
Nicely truncate a string.
int l = s.length();
if (s.length() <= n)
    return s;
int sufflen = suffix.length();
for (int i = n - sufflen; i > 0; i--) {
    char ch = s.charAt(i);
    if (Character.isWhitespace(ch)) {
        return s.substring(0, i) + suffix;
...
StringtruncateNicely(String str, int lower, int upper, String appendToEnd)
This method based on code from the String taglib at Apache Jakarta.
String str2 = removeHTML(str, false);
boolean diff = (str2.length() < str.length());
if (upper < lower) {
    upper = lower;
if (str2.length() > upper) {
    int loc;
    loc = str2.lastIndexOf(' ', upper);
...
StringtruncateNumberSign(String s)
truncate Number Sign
if (s.contains("#")) {
    int i = s.indexOf("#");
    return s.substring(0, i);
} else {
    return s;
StringtruncateOnlyText(String text, int position)
Truncate a text with HTML tags up to the given position.
StringBuilder t = new StringBuilder();
int start = 0;
int end = 0;
int s = 0;
int i = 0, l = 0;
int e = 0;
char p = ' ';
for (; i < text.length(); i++) {
...
StringtruncateOnSpace(String text, Integer length)
truncate On Space
String retVal = "";
if (text.length() <= length) {
    retVal = text;
} else {
    StringBuffer b = new StringBuffer();
    for (int i = 0; i < text.length(); i++) {
        if (b.length() >= length && Character.isWhitespace(text.charAt(i))) { 
            b.append("...");
...
StringtruncateOrPadString(String str)
Truncates a String to a specific length or pads it with extra spaces.
StringBuffer temp;
if (str == null) {
    temp = new StringBuffer();
} else {
    temp = new StringBuffer(str);
int diff = STR_LENGTH - temp.length();
if (diff > 0) {
...