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

StringtruncateString(String text, int truncateAt)
Truncates a string.
if (text == null)
    return null;
if (truncateAt < _TRUNCATE_AT_MINIMUM)
    truncateAt = _TRUNCATE_AT_MINIMUM;
if (text.length() > truncateAt) {
    text = text.substring(0, truncateAt - _ELLIPSIS_LENGTH) + _ELLIPSIS;
return text;
...
StringtruncateStringAt(String source, int len)
Returns a new string that is the first len chars of source
if (source.length() < len) {
    return source;
return source.substring(0, len);
StringtruncateStringChars(String strIn, String substituteChars, int maxChars, boolean keepRightSide)
Truncates a string to the number of characters in maxChars The character count includes the substitute characters (which will only be added if the string is truncated)
if (strIn.length() <= maxChars) {
    return strIn;
strIn = strIn.replaceAll(" ", "");
if (strIn.length() <= maxChars) {
    return strIn;
if (keepRightSide) {
...
StringtruncateStringToUtf8(final String original, final int maxBytes)
Truncates the specified string to fit in the specified maximum number of UTF-8 bytes.
final int length = original.length();
int newLength = 0;
int currentBytes = 0;
while (newLength < length) {
    final char c = original.charAt(newLength);
    boolean isSurrogate = false;
    if (c <= 0x7f) {
        ++currentBytes;
...
StringtruncateSubjectText(String subject)
truncate Subject Text
int limit = 60;
if (subject.length() > limit) {
    subject = subject.substring(0, limit) + "...";
return subject;
StringtruncateTestName(String test)
truncate Test Name
return (test.length() <= 27) ? test : test.substring(0, 28);
StringtruncateText(String str, int lower, int upper, String appendToEnd)
truncate Text
String str2 = removeHTML(str, false);
if (upper < lower) {
    upper = lower;
if (str2.length() > upper) {
    int loc;
    loc = str2.lastIndexOf(' ', upper);
    if (loc >= lower) {
...
StringtruncateText(String text, int maxCharLength)
truncate Text
return truncateText(text, maxCharLength, "...");
StringtruncateTime(String dateString)
truncate Time
String retVal = dateString;
if (dateString != null) {
    int i = dateString.indexOf(" ");
    if (i > -1) {
        retVal = dateString.substring(0, i);
System.out.println("******* at line 28 of AdministeredItemUtil.java" + retVal);
...
StringtruncateToFirstLine(String string)
Truncate a string to the first '\n' character.
int newLineIndex = string.indexOf('\n');
if (newLineIndex == -1) {
    return string;
return String.format("%s...", string.substring(0, newLineIndex));